public member function
<regex>

std::basic_regex::basic_regex

(1)
basic_regex();
(2)
basic_regex (const basic_regex& rgx);
(3)
basic_regex (basic_regex&& rgx) noexcept;
(4)
explicit basic_regex ( const charT* str, flag_type flags = ECMAScript );
(5)
basic_regex ( const charT* str, size_t len, flag_type flags = ECMAScript );
(6)
template <class ST, class SA>
  explicit basic_regex ( const basic_string<charT,ST,SA>& str, flag_type flags = ECMAScript );
(7)
template <class ForwardIterator>
  basic_regex (ForwardIterator first, ForwardIterator last, flag_type flags = ECMAScript );
(8)
basic_regex (initializer_list<charT> il, flag_type flags = ECMAScript );
Construct regex
Constructs a basic_regex object:
default constructor (1)
The regular expression does not match any character sequence.
copy/move constructor (2) and (3)
The basic_regex object acquires rgx properties.
initialization constructors (4), (5), (6), (7) and (8)
The regular expression is initialized to a sequence of characters, that is to be interpreted according to the flags specified in f.

If the syntax used in the sequence of characters has some syntax error, the constructor throws a regex_error exception.

Parameters

rgx
Another basic_regex object of the same type (with the same template parameters).
str
A string with the regular expression (the pattern). See ECMAScript syntax for more details on the syntax to be used (if that grammar is selected).
charT is the first class template paramameter (the character type).
flags
Flags used to interpret the regular expression.
One or more of these constants can be combined (using the bitwise OR operator, |) to form a valid bitmask value of type flag_type:

flag*effects on syntaxNotes
icaseCase insensitiveRegular expressions match without regard to case.
nosubsNo sub-expressionsThe match_results structure will not contain sub-expression matches.
optimizeOptimize matchingMatching efficiency is preferred over efficiency constructing regex objects.
collateLocale sensitivenessCharacter ranges, like "[a-b]", are affected by locale.
ECMAScriptECMAScript grammarThe regular expression follows one of these grammars.
One (and only one) of these six grammar flags needs to be set for the bitmask to have a valid value.
By default, ECMAScript is selected.
basicBasic POSIX grammar
extendedExtended POSIX grammar
awkAwk POSIX grammar
grepGrep POSIX grammar
egrepEgrep POSIX grammar
* These bitmask flag names are member constants of basic_regex. They are also availabe as global constants under the std::regex_constants namespace (see regex_constants for more details).
flag_type is a member type, defined as an alias of the bitmask type regex_constants::syntax_option_type.
len
Length of string p, with p being an array of characters, not necessarily null-terminated.
size_t is an unsigned integral type.
first, last
Forward iterators to the initial and final positions in a range of characters. The range used is [first,last), which includes all the characters between first and last, including the character pointed by first but not the character pointed by last.
The function template type can be any type of forward iterator to characters.
il
An initializer_list of characters. These objects are automatically constructed from initializer list declarators.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// basic_regex constructors
// note: using regex, a standard alias of basic_regex<char>
#include <iostream>
#include <string>
#include <regex>

int main ()
{

  std::string pattern = "^.*$";

  std::regex first;                                        // default
  std::regex second = first;                               // copy
  std::regex third (pattern);                              // string object initialization
  std::regex fourth ("<[^>]>");                            // string literal initialization
  std::regex fifth (pattern.begin(),pattern.end());        // range initialization
  std::regex sixth {'.','+'};                              // initializer_list initialization

  std::regex seventh ("[0-9A-Z]+", std::regex::ECMAScript);// with syntax option

  using namespace std::regex_constants;                    // introducing constants namespace
  std::regex eighth ("[0-9A-Z]+", ECMAScript);             // same as seventh

  std::regex ninth ("\\bd\\w+", ECMAScript | icase );      // multiple flags

  std::string subject = "Duddy the duck";
  std::string replacement = "yup";

  std::cout << std::regex_replace (subject, ninth, replacement);
  std::cout << std::endl;

  return 0;
}


Output:
yup the yup

See also