There's more...

There are multiple versions of regular expressions, and the C++ standard library supports six of them: ECMAScript, basic POSIX, extended POSIX, awk, grep, and egrep (grep with option -E). The default grammar used is ECMAScript, and in order to use another, you explicitly have to specify the grammar when defining the regular expression. In addition to specifying the grammar, you can also specify parsing options, such as matching by ignoring the case.

The standard library provides more classes and algorithms than what we have seen so far. The main classes available in the library are the following (all of them are class templates and, for convenience, typedefs are provided for different character types):

  • The class template std::basic_regex defines the regular expression object:
        typedef basic_regex<char>    regex; 
typedef basic_regex<wchar_t> wregex;
  • The class template std::sub_match represents a sequence of characters that matches a capture group; this class is actually derived from std::pair, and its first and second members represent iterators to the first and the one-past-end characters in the match sequence; if there is no match sequence, the two iterators are equal:
        typedef sub_match<const char *>            csub_match; 
typedef sub_match<const wchar_t *> wcsub_match;
typedef sub_match<string::const_iterator> ssub_match;
typedef sub_match<wstring::const_iterator> wssub_match;
  • The class template std::match_results is a collection of matches; the first element is always a full match in the target, and the other elements are matches of subexpressions:
        typedef match_results<const char *>            cmatch; 
typedef match_results<const wchar_t *> wcmatch;
typedef match_results<string::const_iterator> smatch;
typedef match_results<wstring::const_iterator> wsmatch;

The algorithms available in the regular expressions standard library are the following:

  • std::regex_match(): This tries to match a regular expression (represented by a std::basic_regex instance) to an entire string.
  • std::regex_search(): This tries to match a regular expression (represented by a std::basic_regex instance) to a part of a string (including the entire string).
  • std::regex_replace(): This replaces matches from a regular expression according to a specified format.

The iterators available in the regular expressions standard library are the following:

  • std::regex_interator: A constant forward iterator used to iterate through the occurrences of a pattern in a string. It has a pointer to an std::basic_regex that must live until the iterator is destroyed. Upon creation and when incremented, the iterator calls std::regex_search() and stores a copy of the std::match_results object returned by the algorithm.
  • std::regex_token_iterator: A constant forward iterator used to iterate through the submatches of every match of a regular expression in a string. Internally, it uses an std::regex_iterator to step through the submatches. Since it stores a pointer to an std::basic_regex instance, the regular expression object must live until the iterator is destroyed.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset