Searching for Pattern Matches
RWURegularExpression provides two interfaces for searching strings for occurrences of regular expression pattern matches:
matchAt() and
search().
The overloaded
matchAt() methods test whether a match starts at a specified position in the input string. Positions are specified using
RWUConstStringIterator instances. For example, assuming
pattern is an
RWUString representing a regular expression pattern and
str is an
RWUString representing the input string, this code tests for a match of
pattern at position
3 in
str:
RWURegularExpression r(pattern);
RWUConstStringIterator pos =
str.beginCodePointIterator().advanceCodePoints(3);
RWURegexResult result = r.matchAt(str, pos);
Matches that may begin before or after position 3 are not reported. Similarly, this code tests for a match of pattern at position 3 in str, and not extending beyond position 8:
RWURegularExpression r(pattern);
RWUConstStringIterator pos =
str.beginCodePointIterator().advanceCodePoints(3);
RWUConstStringIterator end =
str.beginCodePointIterator().advanceCodePoints(8);
RWURegexResult result = r.matchAt(str, pos, end);
Similarly, the overloaded search() methods search an input string for an occurrence of a regular expression pattern. For instance:
RWURegularExpression r(pattern);
RWURegexResult result = r.search(str);
By default, the search begins at the beginning of the string, and continues until either the end of the string is reached, or a match is found. Optional arguments allow you to specify other start and end positions for the search. For example, this code begins searching at position 5, and continues until either position 21 is reached, or a match is found:
RWURegularExpression r(pattern);
RWUConstStringIterator start =
str.beginCodePointIterator().advanceCodePoints(5);
RWUConstStringIterator end =
str.beginCodePointIterator().advanceCodePoints(21);
RWURegexResult result = r.search(str, start, end);