matching.qbk 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 Matching and Searching]
  8. [h2 Overview]
  9. Once you have created a regex object, you can use the _regex_match_ and _regex_search_ algorithms to find patterns
  10. in strings. This page covers the basics of regex matching and searching. In all cases, if you are familiar with
  11. how _regex_match_ and _regex_search_ in the _regexpp_ library work, xpressive's versions work the same way.
  12. [h2 Seeing if a String Matches a Regex]
  13. The _regex_match_ algorithm checks to see if a regex matches a given input.
  14. [warning The _regex_match_ algorithm will only report success if the regex matches the ['whole input],
  15. from beginning to end. If the regex matches only a part of the input, _regex_match_ will return false. If you
  16. want to search through the string looking for sub-strings that the regex matches, use the _regex_search_
  17. algorithm.]
  18. The input can be a bidirectional range such as `std::string`, a C-style null-terminated string or a pair of
  19. iterators. In all cases, the type of the iterator used to traverse the input sequence must match the iterator
  20. type used to declare the regex object. (You can use the table in the
  21. [link boost_xpressive.user_s_guide.quick_start.know_your_iterator_type Quick Start] to find the correct regex
  22. type for your iterator.)
  23. cregex cre = +_w; // this regex can match C-style strings
  24. sregex sre = +_w; // this regex can match std::strings
  25. if( regex_match( "hello", cre ) ) // OK
  26. { /*...*/ }
  27. if( regex_match( std::string("hello"), sre ) ) // OK
  28. { /*...*/ }
  29. if( regex_match( "hello", sre ) ) // ERROR! iterator mis-match!
  30. { /*...*/ }
  31. The _regex_match_ algorithm optionally accepts a _match_results_ struct as an out parameter. If given, the _regex_match_
  32. algorithm fills in the _match_results_ struct with information about which parts of the regex matched which
  33. parts of the input.
  34. cmatch what;
  35. cregex cre = +(s1= _w);
  36. // store the results of the regex_match in "what"
  37. if( regex_match( "hello", what, cre ) )
  38. {
  39. std::cout << what[1] << '\n'; // prints "o"
  40. }
  41. The _regex_match_ algorithm also optionally accepts a _match_flag_type_ bitmask. With _match_flag_type_, you can
  42. control certain aspects of how the match is evaluated. See the _match_flag_type_ reference for a complete list
  43. of the flags and their meanings.
  44. std::string str("hello");
  45. sregex sre = bol >> +_w;
  46. // match_not_bol means that "bol" should not match at [begin,begin)
  47. if( regex_match( str.begin(), str.end(), sre, regex_constants::match_not_bol ) )
  48. {
  49. // should never get here!!!
  50. }
  51. Click [link boost_xpressive.user_s_guide.examples.see_if_a_whole_string_matches_a_regex here] to see a complete
  52. example program that shows how to use _regex_match_. And check the _regex_match_ reference to see a complete list
  53. of the available overloads.
  54. [h2 Searching for Matching Sub-Strings]
  55. Use _regex_search_ when you want to know if an input sequence contains a sub-sequence that a regex matches.
  56. _regex_search_ will try to match the regex at the beginning of the input sequence and scan forward in the
  57. sequence until it either finds a match or exhausts the sequence.
  58. In all other regards, _regex_search_ behaves like _regex_match_ ['(see above)]. In particular, it can operate
  59. on a bidirectional range such as `std::string`, C-style null-terminated strings or iterator ranges. The same
  60. care must be taken to ensure that the iterator type of your regex matches the iterator type of your input
  61. sequence. As with _regex_match_, you can optionally provide a _match_results_ struct to receive the results
  62. of the search, and a _match_flag_type_ bitmask to control how the match is evaluated.
  63. Click [link boost_xpressive.user_s_guide.examples.see_if_a_string_contains_a_sub_string_that_matches_a_regex here]
  64. to see a complete example program that shows how to use _regex_search_. And check the _regex_search_ reference to
  65. see a complete list of the available overloads.
  66. [endsect]