C++ Reference ยท Strings and I/O
C++ std::regex class
Reference for std::regex and <regex>: building patterns, ECMAScript syntax and flags, error handling with regex_error, raw strings and performance costs.
std::regex (header <regex>, C++11) is the standard library's regular expression engine. A std::regex object holds a compiled pattern. You pass it to the algorithms std::regex_match, std::regex_search and std::regex_replace, or to the iterators std::sregex_iterator and std::sregex_token_iterator, which do the actual matching on a string.
Compiling the pattern is the expensive part, so construct a regex once and reuse it instead of creating it inside a loop. The default grammar is ECMAScript, which is the same family of syntax JavaScript uses: character classes, groups, alternation, quantifiers, anchors, lazy quantifiers and look-ahead. Look-behind and named groups are not supported.
#include <regex> std::regex re(R"((\d+)-(\d+))"); // raw string: no doubled backslashes
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
std::basic_regex<char> regex; // typedef std::regex (std::wregex for wchar_t) explicit regex(const std::string& pattern, std::regex_constants::syntax_option_type flags = std::regex_constants::ECMAScript);
Flag (std::regex::...) |
Description |
|---|---|
ECMAScript |
The default grammar. |
basic, extended, awk, grep, egrep |
POSIX-style grammars. Choose one grammar only. |
icase |
Case-insensitive matching. |
nosubs |
Do not store sub-matches: only the whole match is reported. |
optimize |
Hint to favor matching speed over compile speed (often no effect). |
collate |
Make [a-z] ranges locale-sensitive. |
multiline |
^ and $ match at line boundaries (C++17). |
Errors: an invalid pattern throws std::regex_error, whose code() is one of the constants in std::regex_constants (error_paren, error_brack, error_badrepeat, error_escape, ...).
Usage notes
- Use raw string literals.
R"(\d+)"is far easier to read than"\\d+". If the pattern contains)", add a delimiter:R"re(...)re". - Compile once. Make the regex
const,staticor a class member; never build it per iteration of a loop or per call of a hot function. - Never pass a temporary string to
regex_searchwith amatch_results. The overload takingconst std::string&&is deleted, because the result would point into a destroyed string. Store the string in a variable first. - Known limits. Large inputs and certain patterns can recurse deeply in libstdc++ and overflow the stack; avoid regexes on megabytes of data. Compilation and matching are slower than in dedicated libraries such as RE2 or PCRE.
- Not a parser. HTML, JSON, nested parentheses and similar recursive grammars cannot be handled reliably with regular expressions. Use a real parser.
- Unicode.
std::regexworks oncharunits, so a multi-byte UTF-8 character counts as several characters..may match half of a character. - Compile time.
<regex>is a heavy header. In our editor, programs that use it take noticeably longer to compile than most examples (several seconds). - C++17 adds the
multilineflag. The library has no named capture groups or look-behind in any standard.