Professional C__ - Marc Gregoire [258]
typedef regex_token_iterator typedef regex_token_iterator typedef regex_token_iterator The following sections explain the regex_match(), regex_search() and regex_replace() algorithms and the regex_iterator and regex_token_iterator. regex_match() The regex_match() algorithm can be used to compare a given source string with a regular expression pattern and will return true if the pattern matches the entire source string, false otherwise. It is very easy to use. There are six versions of the regex_match() algorithm accepting different kinds of arguments: template bool regex_match(BidirectionalIterator first, BidirectionalIterator last, match_results const basic_regex regex_constants::match_flag_type flags = regex_constants::match_default); template bool regex_match(BidirectionalIterator first, BidirectionalIterator last, const basic_regex regex_constants::match_flag_type flags = regex_constants::match_default); template bool regex_match(const charT* str, match_results const basic_regex regex_constants::match_flag_type flags = regex_constants::match_default); template bool regex_match(const basic_string match_results< typename basic_string Allocator>& m, const basic_regex regex_constants::match_flag_type flags = regex_constants::match_default); template bool regex_match(const charT* str, const basic_regex regex_constants::match_flag_type flags = regex_constants::match_default); template bool regex_match(const basic_string const basic_regex regex_constants::match_flag_type flags = regex_constants::match_default); Some of them accept a start and end iterator into a source string where pattern matching should start and end; others accept a simple string or a character array as source. All of them require a basic_regex as one of their arguments, which represents the regular expression. All variations return true when the entire source string matches the pattern, false otherwise; and all accept a combination of flags to specify options for the matching algorithm. In most cases this can be left as match_default. Consult the Standard Library Reference resource on the website for more details. Both regex_match() and regex_search() described in a later section can use an optional match_results object. If the algorithms return false, you are only allowed to call match_results::empty() or match_results::size(); anything else is undefined. When the algorithms return true, a match is found and you can inspect the match_results object for what exactly got matched. How to do this is explained with examples in the following sections. regex_match() Example The function prototypes in the previous section might look complicated, but actually using regex_match() is not complicated at all. Suppose you want to write a program that asks the user to enter a date in the following format year/month/day where year is four digits, month is a number between 1 and 12, and day is a number between 1 and 31. You can use a regular expression together with the regex_match() algorithm to validate the user input as follows. The details of the regular expression are explained after the code: regex r("^\d{4}/(?:0?[1-9]|1[0-2])/(?:0?[1-9]|[1-2][0-9]|3[0-1])$"); while (true) { cout << "Enter a date (year/month/day) (q=quit): "; string str; if (!getline(cin, str) || str == "q") break;