Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/xpressive/doc/results.qbk @ 33

Last change on this file since 33 was 29, checked in by landauf, 16 years ago

updated boost from 1_33_1 to 1_34_1

File size: 4.2 KB
Line 
1[section Accessing Results]
2
3[h2 Overview]
4
5Sometimes, it is not enough to know simply whether a _regex_match_ or _regex_search_ was successful or not. If
6you pass an object of type _match_results_ to _regex_match_ or _regex_search_, then after the algorithm has completed
7successfully the _match_results_ will contain extra information about which parts of the regex matched which parts
8of the sequence. In Perl, these sub-sequences are called ['back-references], and they are stored in the variables
9[^$1], [^$2], etc. In xpressive, they are objects of type _sub_match_, and they are stored in the _match_results_
10structure, which acts as a vector of _sub_match_ objects.
11
12[h2 match_results]
13
14So, you've passed a _match_results_ object to a regex algorithm, and the algorithm has succeeded. Now you want
15to examine the results. Most of what you'll be doing with the _match_results_ object is indexing into it to access
16its internally stored _sub_match_ objects, but there are a few other things you can do with a _match_results_
17object besides.
18
19The table below shows how to access the information stored in a _match_results_ object named `what`.
20
21[table match_results<> Accessors
22    [[Accessor]             [Effects]]
23    [[`what.size()`]        [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.]]
24    [[`what[n]`]            [Returns the ['n]-th sub-match.]]
25    [[`what.length(n)`]     [Returns the length of the ['n]-th sub-match. Same as `what[n].length()`.]]
26    [[`what.position(n)`]   [Returns the offset into the input sequence at which the ['n]-th sub-match begins.]]
27    [[`what.str(n)`]        [Returns a `std::basic_string<>` constructed from the ['n]-th sub-match. Same as `what[n].str()`.]]
28    [[`what.prefix()`]      [Returns a _sub_match_ object which represents the sub-sequence from the beginning of the input sequence to the start of the full match.]]
29    [[`what.suffix()`]      [Returns a _sub_match_ object which represents the sub-sequence from the end of the full match to the end of the input sequence.]]
30    [[`what.regex_id()`]    [Returns the `regex_id` of the _basic_regex_ object that was last used with this _match_results_ object.]]
31]
32
33There is more you can do with the _match_results_ object, but that will be covered when we talk about
34[link boost_xpressive.user_s_guide.grammars_and_nested_matches Grammars and Nested Matches].
35
36[h2 sub_match]
37
38When you index into a _match_results_ object, you get back a _sub_match_ object. A _sub_match_ is basically a pair
39of iterators. It is defined like this:
40
41    template< class BidirectionalIterator >
42    struct sub_match
43        : std::pair< BidirectionalIterator, BidirectionalIterator >
44    {
45        bool matched;
46        // ...
47    };
48
49Since it inherits publicaly from `std::pair<>`, _sub_match_ has `first` and `second` data members of type
50`BidirectionalIterator`. These are the beginning and end of the sub-sequence this _sub_match_ represents.
51_sub_match_ also has a Boolean `matched` data member, which is true if this _sub_match_ participated in the full
52match.
53
54The following table shows how you might access the information stored in a _sub_match_ object called `sub`.
55
56[table sub_match<> Accessors
57    [[Accessor]             [Effects]]
58    [[`sub.length()`]       [Returns the length of the sub-match. Same as `std::distance(sub.first,sub.second)`.]]
59    [[`sub.str()`]          [Returns a `std::basic_string<>` constructed from the sub-match. Same as `std::basic_string<char_type>(sub.first,sub.second)`.]]
60    [[`sub.compare(str)`]   [Performs a string comparison between the sub-match and `str`, where `str` can be a `std::basic_string<>`, C-style null-terminated string, or another sub-match. Same as `sub.str().compare(str)`.]]
61]
62
63[h2 __alert__ Results Invalidation __alert__]
64
65Results are stored as iterators into the input sequence. Anything which invalidates
66the input sequence will invalidate the match results. For instance, if you match a `std::string` object,
67the results are only valid until your next call to a non-const member function of that `std::string` object.
68After that, the results held by the _match_results_ object are invalid. Don't use them!
69
70[endsect]
Note: See TracBrowser for help on using the repository browser.