# terminate called after throwing an instance of 'std::regex_error' what(): regex_error / Unmatched ( or \(

**Code:** `regex_error` · **Category:** C/C++ · **Seen on:** C/C++, GCC

The regular expression string passed to `std::regex` is invalid.

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/std-regex-error)
