results.qbk 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. [/
  2. / Copyright (c) 2008 Eric Niebler
  3. /
  4. / Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. / file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. /]
  7. [section Accessing Results]
  8. [h2 Overview]
  9. Sometimes, it is not enough to know simply whether a _regex_match_ or _regex_search_ was successful or not. If
  10. you pass an object of type _match_results_ to _regex_match_ or _regex_search_, then after the algorithm has completed
  11. successfully the _match_results_ will contain extra information about which parts of the regex matched which parts
  12. of the sequence. In Perl, these sub-sequences are called ['back-references], and they are stored in the variables
  13. [^$1], [^$2], etc. In xpressive, they are objects of type _sub_match_, and they are stored in the _match_results_
  14. structure, which acts as a vector of _sub_match_ objects.
  15. [h2 match_results]
  16. So, you've passed a _match_results_ object to a regex algorithm, and the algorithm has succeeded. Now you want
  17. to examine the results. Most of what you'll be doing with the _match_results_ object is indexing into it to access
  18. its internally stored _sub_match_ objects, but there are a few other things you can do with a _match_results_
  19. object besides.
  20. The table below shows how to access the information stored in a _match_results_ object named `what`.
  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. There is more you can do with the _match_results_ object, but that will be covered when we talk about
  33. [link boost_xpressive.user_s_guide.grammars_and_nested_matches Grammars and Nested Matches].
  34. [h2 sub_match]
  35. When you index into a _match_results_ object, you get back a _sub_match_ object. A _sub_match_ is basically a pair
  36. of iterators. It is defined like this:
  37. template< class BidirectionalIterator >
  38. struct sub_match
  39. : std::pair< BidirectionalIterator, BidirectionalIterator >
  40. {
  41. bool matched;
  42. // ...
  43. };
  44. Since it inherits publicaly from `std::pair<>`, _sub_match_ has `first` and `second` data members of type
  45. `BidirectionalIterator`. These are the beginning and end of the sub-sequence this _sub_match_ represents.
  46. _sub_match_ also has a Boolean `matched` data member, which is true if this _sub_match_ participated in the full
  47. match.
  48. The following table shows how you might access the information stored in a _sub_match_ object called `sub`.
  49. [table sub_match<> Accessors
  50. [[Accessor] [Effects]]
  51. [[`sub.length()`] [Returns the length of the sub-match. Same as `std::distance(sub.first,sub.second)`.]]
  52. [[`sub.str()`] [Returns a `std::basic_string<>` constructed from the sub-match. Same as `std::basic_string<char_type>(sub.first,sub.second)`.]]
  53. [[`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)`.]]
  54. ]
  55. [h2 __alert__ Results Invalidation __alert__]
  56. Results are stored as iterators into the input sequence. Anything which invalidates
  57. the input sequence will invalidate the match results. For instance, if you match a `std::string` object,
  58. the results are only valid until your next call to a non-const member function of that `std::string` object.
  59. After that, the results held by the _match_results_ object are invalid. Don't use them!
  60. [endsect]