![]() |
Home | Libraries | People | FAQ | More |
Sometimes, it is not enough to know simply whether a
or regex_match()
was successful or not. If you pass an object of type regex_search()
to match_results<>
or regex_match()
,
then after the algorithm has completed successfully the regex_search()
will contain extra information about which parts of the regex matched which
parts of the sequence. In Perl, these sub-sequences are called back-references,
and they are stored in the variables match_results<>
$1
, $2
,
etc. In xpressive, they are objects of type
,
and they are stored in the sub_match<>
structure, which acts as a vector of match_results<>
objects.
sub_match<>
So, you've passed a
object to a regex algorithm, and the algorithm has succeeded. Now you want
to examine the results. Most of what you'll be doing with the match_results<>
object is indexing into it to access its internally stored match_results<>
objects, but there are a few other things you can do with a sub_match<>
object besides.
match_results<>
The table below shows how to access the information stored in a
object named match_results<>
what
.
Table 1.5. match_results<> Accessors
Accessor |
Effects |
---|---|
|
Returns the number of sub-matches, which is always greater than zero after a successful match because the full match is stored in the zero-th sub-match. |
|
Returns the n-th sub-match. |
|
Returns the length of the n-th sub-match.
Same as |
|
Returns the offset into the input sequence at which the n-th sub-match begins. |
|
Returns a |
|
Returns a |
|
Returns a |
|
Returns the |
There is more you can do with the
object, but that will be covered when we talk about Grammars
and Nested Matches.
match_results<>
When you index into a
object, you get back a match_results<>
object. A sub_match<>
is basically a pair of iterators. It is defined like this:
sub_match<>
template< class BidirectionalIterator > struct sub_match : std::pair< BidirectionalIterator, BidirectionalIterator > { bool matched; // ... };
Since it inherits publicaly from std::pair<>
,
has sub_match<>
first
and second
data members of type BidirectionalIterator
. These are the beginning
and end of the sub-sequence this
represents. sub_match<>
also has a Boolean sub_match<>
matched
data member, which is true if this
participated in the full match.
sub_match<>
The following table shows how you might access the information stored in
a
object called sub_match<>
sub
.
Table 1.6. sub_match<> Accessors
Accessor |
Effects |
---|---|
|
Returns the length of the sub-match. Same as |
|
Returns a |
|
Performs a string comparison between the sub-match and |
Results are stored as iterators into the input sequence. Anything which invalidates
the input sequence will invalidate the match results. For instance, if you
match a std::string
object, the results are only valid
until your next call to a non-const member function of that std::string
object. After that, the results held by the
object are invalid. Don't use them!
match_results<>