regex_error C/C++ Origin: Application & API OSI layer 7 · Application Last updated 2 Dec 2026

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.

Exact message you may see regex_error UnmatchedThe expression contained mismatched ( and )Invalid escape sequence regex
Seen on: C/C++ GCC

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

  1. Use raw string literals: std::regex re(R"(\d+)")
  2. Test the pattern on regex101 (ECMAScript flavour)
  3. Catch std::regex_error and print e.code()

Detailed fix by platform

C++

  1. try { std::regex re(pattern); } catch (const std::regex_error& e) { std::cerr << e.what() << " code " << e.code(); }

How to diagnose

  1. Pattern — Balanced?
  2. Literal — Raw string?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

bad_cast — terminate called after throwing an instance of 'std::bad_cast' what(): std::bad_cast (C/C++)bad_castbad_function_call — terminate called after throwing 'std::bad_function_call' what(): bad_function_call (C/C++)bad_function_callbad_variant_access — terminate called after throwing 'std::bad_variant_access' / 'std::bad_optional_access' (C/C++)bad_variant_accessfilesystem_error — terminate called after throwing an instance of 'std::filesystem::filesystem_error' what(): filesystem error: cannot ... No such file or directory (C/C++)filesystem_errorNo associated state — terminate called after throwing an instance of 'std::future_error' what(): std::future_error: No associated state (C/C++)No associated statestd::bad_alloc — C++: terminate called after throwing an instance of 'std::bad_alloc' — what(): std::bad_alloc (C/C++)std::bad_allocstoi — terminate called after throwing 'std::invalid_argument' what(): stoi (C/C++)stoiterminate called after throwing an instance of — C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check (C/C++)terminate called afte…regex_err…

Explore the map of this error Full error map →

🧠 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.