DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
A Portable C++ Regular Expression Facility - Regex(3C++)

Regex iteration

We mentioned above that match always finds the leftmost longest matching substring in the target string. Sometimes we may want to find all the longest matching substrings. This is easily done using the iterator class Regexiter.

There are two possible interpretations of the phrase ``all matching substrings.'' Under non-overlapping iteration (the default), successive matches are disjoint in the target string.

       Regex r("(foo)|(oof)");
       Regexiter i(r, "foofoo");
           // iterate r over target string "foofoo"
       Substrinfo ss;
       while (ss = i.next())
           cout << ss.i << ', ';

This program prints out ``0, 3,'' corresponding to the matches ``foo'' and ``foo''. Under overlapping iteration, successive matches are allowed to partially overlap the results of earlier matches.

       Regex r("(foo)|(oof)");
       Regexiter i(r, "foofoo", Regexiter::overlapping);
       // ... remainder as above

This program prints ``0, 1, 3,'' corresponding to the matches ``foo'', ``oof'', and ``foo''.

As with match, subexpression matching information can be retrieved by supplying an optional Subex argument to next.

       Regex r("(foo)|(oof)");
       Regexiter i(r, "foofoof", Regexiter::overlapping);
       Subex subs;
       while (i.next(subs)) {
           String s;
           Substrinfo ss = subs(2, s);
           if (ss) {
               cout << '\ "' << s << "\ " at " << ss.i;
               cout << " matched subexpression 2\ n";
           }
       }

This prints out the following:

   "oof" at 1 matched subexpression 2
   "oof" at 4 matched subexpression 2

Next topic: A final example
Previous topic: Subexpressions

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004