public member function
<regex>

std::basic_regex::assign

(1)
basic_regex& assign (const basic_regex& rgx);
(2)
basic_regex& assign (basic_regex&& rgx) noexcept;
(3)
basic_regex& assign (const charT* str, flag_type flags = ECMAScript);
(4)
basic_regex& assign (const charT* str, size_t len, flag_type flags = ECMAScript);
(5)
template <class ST, class SA>
  basic_regex& assign (const basic_string<charT,ST,SA>& str, flag_type flags = ECMAScript);
(6)
template <class InputIterator>
  basic_regex& assign (InputIterator first, InputIterator last, flag_type f = ECMAScript);
(7)
basic_regex& assign (initializer_list<charT> il, flag_type flags = ECMAScript);
Assign regular expression
Assigns a new regular expression (pattern) to the object, overwriting any previous value.

With versions (1) and (2) the object acquires the syntax option flags of rgx. With the other versions, syntax flags can be specified as the last argument.

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

Parameters

rgx
Another basic_regex object of the same type.
str
A string with the pattern to use as regular expression.
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 str, with str 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.

Return value

*this

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
// basic_regex::assign
// note: using regex, a standard alias of basic_regex<char>
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string pattern ("six");
  std::regex first;
  std::regex second (pattern);


  first.assign (second);
  second.assign ("[0-9A-F]+", std::regex::ECMAScript);
  second.assign ({'^','a'}, std::regex::ECMAScript | std::regex::icase );

  using namespace std::regex_constants;                // introducing constants 
  second.assign (pattern, ECMAScript | icase );

  std::string subject = "Sixty six";
  std::string replacement = "seven";

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

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

  return 0;
}


Output:
seventy seven
Sixty seven

See also