terminate called after throwing an instance of 'std::regex_error' what(): regex_error / Unmatched ( or \(
The regular expression string passed to std::regex is invalid.
regex_error UnmatchedThe expression contained mismatched ( and )Invalid escape sequence regex Meaning
Constructing the regex throws std::regex_error with an error code (brackets, escapes, ranges). Backslashes must be doubled in string literals ("\\d+"), and ECMAScript syntax is used by default.
Common causes
- Unbalanced brackets or parentheses
- Single backslash in a C++ string literal
- Unsupported syntax (lookbehind, possessive quantifiers)
- Special characters not escaped
⚡ Quick fix
- Use raw string literals:
std::regex re(R"(\d+)") - Test the pattern on regex101 (ECMAScript flavour)
- Catch
std::regex_errorand printe.code()
Detailed fix by platform
C++
try { std::regex re(pattern); } catch (const std::regex_error& e) { std::cerr << e.what() << " code " << e.code(); }
How to diagnose
- Pattern — Balanced?
- Literal — Raw string?
- Syntax — ECMAScript only?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- bad_cast terminate called after throwing an instance of 'std::bad_cast' what(): std::bad_cast
- bad_function_call terminate called after throwing 'std::bad_function_call' what(): bad_function_call
- bad_variant_access terminate called after throwing 'std::bad_variant_access' / 'std::bad_optional_access'
Most viewed in C/C++
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 2 Dec 2026