The C++ standard library as defined in the C++11 standard and later versions provides support for regular expressions in the <regex> header. When this website mentions std::regex, this refers to the Dinkumware implementation of the C++ standard library that is included with Visual C++ 2010 and later. It is also supported by C++Builder XE3 and later when targeting Win64.
C++Builder 10.x and 11 support the Dinkumware implementation std::regex when targeting Win32 if you disable the option to use the classic Borland compiler. When using the classic Borland compiler in C++Builder XE3 and later, you can use boost::regex instead of std::regex. While std::regex as defined in C++11 defines pretty much the same operations and classes as boost::regex, there are a number of important differences in the actual regex flavor. Most importantly the ECMAScript regex syntax in Boost adds a number of features borrowed from Perl that aren’t part of the ECMAScript standard and that aren’t implemented in the Dinkumware library. The topic about Boost has a reference table comparing std::regex and boost::regex.
C++Builder 12 introduces a new "Win64 Modern" target platform. This platform uses the libc++ implementation of the C++ standard library. Other platforms in C++Builder 12 still use the Dinkumware implementation. There are some incompatibilities. Most importantly, libc++ matches the anchors ^ and $ only match at the start and end of the string, while Dinkumware matches them at the start and end of every line. JavaScript matches them only at the start and end of the string by default, but will match them at the start and end of every line if you append the /m flag to your regex. Backreferences to non-participating capturing groups match the empty string with Dinkumware (as they do in JavaScript) but fail to match with libc++ (as they do in most other regex flavors). The empty character class [] fails to match with libc++ (as it does in JavaScript) while it is an error with Dinkumware.
There is also an incompatibility between Visual C++ and C++Builder when using std::wregex using strings of wchar_t. In Visual C++, the shorthands \d, \w, and \s match Unicode characters. In C++Builder the shorthands are always limited to ASCII characters, for both the Dinkumware and libc++ implementations. In JavaScript, \d and \w are ASCII-only while \s matches all Unicode whitespace. This is odd, but all modern browsers follow the spec.
Six different regular expression flavors or grammars are defined in std::regex_constants:
In practice, you’ll mostly use the ECMAScript grammar. It’s the default grammar and offers far more features that the other grammars. Whenever the tutorial on this website mentions std::regex without mentioning any grammars then what is written applies to the ECMAScript grammar and may or may not apply to any of the other grammars. You’ll really only use the other grammars if you want to reuse existing regular expressions from old POSIX code or UNIX scripts.
Most C++ references talk as if C++11 implements regular expressions as defined in the ECMA-262v3 and POSIX standards. But in reality the C++ implementations are loosely based these standards. The two implementations here are actually incompatible between themselves, as discussed in the previous section.
The regex syntax is the same in both implementations, and quite close to ECMA-262v3. The only significant differences are that std::regex supports POSIX classes even in ECMAScript mode, and that it is a bit peculiar about which characters must be escaped (like curly braces and closing square brackets) and which must not be escaped (like letters).
ECMA-262v3 is an older version of the ECMAScript standard now. JavaScript in modern browsers includes features such as named capture, mode modifiers, and Unicode properties. C++ has not adopted these.
Before you can use a regular expression, you have to create an object of the template class std::basic_regex. You can easily do this with the std::regex instantiation of this template class if your subject is an array of char or an std::string object. Use the std::wregex instantiation if your subject is an array of wchar_t of an std::wstring object.
Pass your regex as a string as the first parameter to the constructor. If you want to use a grammar other than ECMAScript then pass the appropriate std::regex_constants member as a second parameter. You can combine this constant with std::regex_constants::icase using bitwise or to make the regex case insensitive. You can also combine it with std::regex_constants::nosubs to turn all capturing groups into non-capturing groups. That makes your regex more efficient if you only care about the overall regex match and don’t want to extract text matched by any of the capturing groups.
Call std::regex_search() with your subject string as the first parameter and the regex object as the second parameter to check whether your regex can match any part of the string. Call std::regex_match() with the same parameters if you want to check whether your regex can match the entire subject string. Since std::regex lacks anchors that exclusively match at the start and end of the string, you have to call regex_match() when using a regex to validate user input.
Both regex_search() and regex_match() return just true or false. To get the part of the string matched by regex_search(), or to get the parts of the string matched by capturing groups when using either function, you need to pass an object of the template class std::match_results as the second parameter. The regex object then becomes the third parameter. Create this object using the default constructor of one of these four template instantiations:
When the function call returns true, you can call the str(), position(), and length() member functions of the match_results object to get the text that was matched, or the starting position and its length of the match relative to the subject string. Call these member functions without a parameter or with 0 as the parameter to get the overall regex match. Call them passing 1 or greater to get the match of a particular capturing group. The size() member function indicates the number of capturing groups plus one for the overall match. Thus you can pass a value up to size()-1 to the other three member functions.
Putting it all together, we can get the text matched by the first capturing group like this:
std::string subject("Name: John Doe");
std::string result;
try {
std::regex re("Name: (.*)");
std::smatch match;
if (std::regex_search(subject, match, re) && match.size() > 1) {
result = match.str(1);
} else {
result = std::string("");
}
} catch (std::regex_error& e) {
// Syntax error in the regular expression
} To find all regex matches in a string, you need to use an iterator. Construct an object of the template class std::regex_iterator using one of these four template instantiations:
Construct one object by calling the constructor with three parameters: a string iterator indicating the starting position of the search, a string iterator indicating the ending position of the search, and the regex object. If there are any matches to be found, the object will hold the first match when constructed. Construct another iterator object using the default constructor to get an end-of-sequence iterator. You can compare the first object to the second to determine whether there are any further matches. As long as the first object is not equal to the second, you can dereference the first object to get a match_results object.
std::string subject("This is a test");
try {
std::regex re("\\w+");
std::sregex_iterator next(subject.begin(), subject.end(), re);
std::sregex_iterator end;
while (next != end) {
std::smatch match = *next;
std::cout << match.str() << "\n"; next++; } } catch (std::regex_error& e) { // Syntax error in the regular expression } To replace all matches in a string, call std::regex_replace() with your subject string as the first parameter, the regex object as the second parameter, and the string with the replacement text as the third parameter. The function returns a new string with the replacements applied.
The replacement text syntax does not depend on the regex grammar. But std::regex does provide two "formats" for the replacement text. The default format is similar but not identical to that of JavaScript.
The only character with a special meaning in the default replacement syntax is the dollar sign. A $ that does not form a replacement text token inserts a single literal dollar sign. $$ also inserts a single literal dollar sign, so you can double up a dollar sign to prevent it from being interpreted as a replacement text token.
You can use $& to insert the whole regex match. 0ドル does the same in libc++ and older versions of Dinkumware (VC++ 2013 and prior) but not in the latest Dinkumware (VC++ 2015 and later).
Use 1ドル through 99ドル to insert the text matched by capturing groups 1 through 99. They insert nothing if there are fewer capturing groups in the regex than the requested number. 10ドル is never seen as 1ドル followed by a literal 0. 123ドル is always seen as a two-digit backreference followed by a literal digit, even if the regex has 123 capturing groups.
$` (dollar backtick) is the part of the string to the left of the match, and $' (dollar quote) is the part of the string to the right of the match.
You can pass std::regex_constants::format_sed as a 4th parameter to std::regex_replace() to change the replacement text format to one that is similar to that of the UNIX sed tool. It is quite limited. In this syntax, the dollar sign is always just a literal. The backslash always escapes the next character. Some escapes have a special meaning.
You can use 0円 to insert the whole regex match. Use 1円 through 9円 to insert the text matched by the first nine capturing groups. They insert nothing if there are fewer capturing groups in the regex than the requested number. There is no way to insert the matches of groups 10 or above. 1円0 is always seen as a backreference to the first group followed by a literal 0.
| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |
| grep | PowerGREP | RegexBuddy | RegexMagic |
| EditPad Lite | EditPad Pro | Google Docs | Google Sheets | LibreOffice | Notepad++ |
| Boost | C# | Delphi | F# | GNU (Linux) | Groovy | ICU (Unicode) | Java | JavaScript | .NET | PCRE (C/C++) | PCRE2 (C/C++) | Perl | PHP | POSIX | PowerShell | Python | Python.NET and IronPython | R | RE2 | Ruby | std::regex | Tcl | TypeScript | VBScript | Visual Basic 6 | Visual Basic (.NET) | wxWidgets | XML Schema | XQuery & XPath | Xojo | XRegExp |
| Google BigQuery | MySQL | Oracle | PostgreSQL |
Page URL: https://www.regular-expressions.info/stdregex.html
Page last updated: 25 June 2025
Site last updated: 29 October 2025
Copyright © 2003-2025 Jan Goyvaerts. All rights reserved.