gular
expression") , which are a kind of mini-language used to perform pattern
matching within strings. Almost all operations with regexes can be
characterized by operating on several of the following objects:

  * **Target sequence** . The character sequence that is searched for a pattern. This may be a range specified by two iterators, a null-terminated character string or a  [ std::string ](/w/cpp/string/basic_string "cpp/string/basic string") . 

  * **Pattern** . This is the regular expression itself. It determines what constitutes a match. It is an object of type  [ std::basic_regex ](/w/cpp/regex/basic_regex "cpp/regex/basic regex") , constructed from a string with special  grammar  . 

  * **Matched array** . The information about matches may be retrieved as an object of type  [ std::match_results ](/w/cpp/regex/match_results "cpp/regex/match results") . 

  * **Replacement string** . This is a string that determines how to replace the matches. 

##  Contents

  * 1  Regular expression grammars 
  * 2  Main classes 
  * 3  Algorithms 
  * 4  Iterators 
  * 5  Exceptions 
  * 6  Traits 
  * 7  Constants 
  * 8  Example 

  
---  
  
###  [ [ edit ](/mwiki/index.php?title=cpp/regex&action=edit&section=1 "Edit
section: Regular expression grammars") ]  Regular expression grammars

Patterns and replacement strings support the following regular expression
grammars:

  * [ Modified ECMAScript regular expression grammar ](/w/cpp/regex/ecmascript "cpp/regex/ecmascript") . This is the default grammar. 
  * [ Basic POSIX regular expression grammar ](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03) . 
  * [ Extended POSIX regular expression grammar ](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04) . 
  * The regular expression grammar used by the  [ awk ](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html#tag_20_06_13_04) utility utility in POSIX. 
  * The regular expression grammar used by the  [ grep ](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html) utility in POSIX. This is effectively the same as the basic POSIX regular expression grammar, with the addition of newline  '  \n  '  as an alternation separator. 
  * The regular expression grammar used by the  grep  utility, with the  \-  E  option, in POSIX. This is effectively the same as the extended POSIX regular expression grammar, with the addition of newline  '  \n  '  as an alternation separator in addition to  '|'  . 

Some grammar variations (such as case-insensitive matching) are also
avaliable, see [ this page ](/w/cpp/regex/basic_regex/constants
"cpp/regex/basic regex/constants") for details.

###  [ [ edit ](/mwiki/index.php?title=cpp/regex&action=edit&section=2 "Edit
section: Main classes") ]  Main classes

These classes encapsulate a regular expression and the results of matching a
regular expression within a target sequence of characters.

[ basic_regex  ](/w/cpp/regex/basic_regex "cpp/regex/basic regex") (C++11)  |  regular expression object   
(class template)  [ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/regex/dsc_basic_regex&action=edit)  
---|---  
[ sub_match  ](/w/cpp/regex/sub_match "cpp/regex/sub match") (C++11)  |  identifies the sequence of characters matched by a sub-expression   
(class template)  [ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/regex/dsc_sub_match&action=edit)  
[ match_results  ](/w/cpp/regex/match_results "cpp/regex/match results") (C++11)  |  identifies one regular expression match, including all sub-expression matches   
(class template)  [ [edit]
](https://en.cppreference.com/mwiki/index.php?title=Template:cpp/regex/dsc_match_results&action=edit)  
  
###  [ [ edit ](/mwiki/index.php?title=cpp/regex&action=edit&section=3 "Edit
section: Algorithms") ]  Algorithms

These functions are used to apply the regular expression encapsulated in a
regex to a target sequence of ch