Regular expressions are a standardized way to express patterns to be matched against sequences of characters.
The standard C++ library provides support for regular expressions in the <regex> header through a series of operations. All these operations make use of some typical regex parameters:
- Target sequence (subject): The sequence of characters searched for the pattern. Generally, this is a range specified by two iterators, but some functions also accept a c-string or a string object instead.
- Regular expression (pattern): The pattern which is searched for in the target sequence. This must be an object of a basic_regex type (such as regex), generally constructed from a string with a special syntax that describes what constitutes a match (see ECMAScript syntax).
- Matches array: Some operations allow to retrieve information about matches. This information is stored in one of the special match_results array types (such as cmatch or smatch).
- Replacement string: Some operations can replace matches. These replacements are specified in strings that allow for a special format (see ECMAScript syntax).
Regex operations are performed using either functions or special iterator adaptors:
- Match sequence (function template)
- Search sequence (function template)
- Replace matched sequence (function template)
- Regex iterator (class template)
- Regex token iterator (class template)
The regex functions and iterators make heavy use of a set of supporting types as arguments and return values:
- Regular expression (class template)
- Match results (class template)
- Sub-expression match (class template)
- Regex traits (class template)
- Regex exception (class)
Some of these types are templates, and have aliases for their most common instantiations:
- Regex (class)
- Regex for wchar_t (class)
- match_results for string literals (class)
- match_results for wide string literals (class)
- match_results for string objects (class)
- match_results for wide string objects (class)
- sub_match for string literals (class)
- sub_match for wide string literals (class)
- sub_match for strings (class)
- sub_match for wide strings (class)
This header also defines a namespace, regex_constants, under which all constant values to be used by the library are located:
- regex constants (namespace)
Regular expressions follow very strict grammars. By default, the functions in this library use the ECMAScript grammar:
- ECMAScript syntax
- ECMAScript regular expressions pattern syntax (syntax specifications)
- Iterator to beginning (function template)
- Iterator to end (function template)