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