Regular expressions library
Curated hub for C++ regex: pattern objects, match/search/replace algorithms, flags, traits, match results, iterators, and string/text boundaries.
The C++ regular expressions library is built around a small set of cooperating pieces: a compiled regex object, algorithms that apply it to character ranges, result objects that describe matches, iterators that walk repeated matches or tokens, and option/flag types that change both syntax and matching behavior. This hub helps you choose the right regex surface before you dive into the lower-level details.
# Start Here
Compile and hold a regex pattern
Start with the regex object when your first question is how a pattern is represented, compiled, or parameterized by syntax options and traits.
basic_regex · syntax_option_type · ECMAScript grammar · regex_traits
Check or search input text
Use the matching algorithms when you need whole-input validation, substring search, or extraction of capture groups from a source range.
Rewrite text with regex replacement
Choose the replace route when the job is transforming text by substituting matched ranges rather than just detecting them.
Iterate over many matches or tokens
Use iterators when you want to traverse every match or every tokenized slice instead of calling search in a loop manually.
Choose syntax and matching options
Use the flags route when behavior depends on grammar flavor, case sensitivity, formatting rules, or how algorithms advance through the input.
Handle regex-specific failures
Start here when pattern compilation fails or when you need to reason about the library's regex-specific error categories.
# Quick Map
| If you need to... | Start with | Why | Common adjacent route |
|---|---|---|---|
| Validate that the entire input matches a pattern | regex_match | `regex_match` is the whole-input check, unlike search which succeeds on any matching subrange. | regex_search |
| Find whether a pattern occurs anywhere inside a string | regex_search | This is the canonical route for substring discovery and capture extraction from larger text. | regex_match |
| Replace matching text with formatted output | regex_replace | The replacement algorithm handles substitution syntax and formatting behavior directly. | format |
| Walk every match across an input range | regex_iterator | Iterators provide repeated-match traversal without hand-written search loops. | regex_token_iterator |
| Split or tokenize text around regex matches or submatches | regex_token_iterator | This is the standard route for token-oriented regex iteration, including delimiter-style behavior. | string |
| Choose grammar flavor or matching/replacement flags | syntax_option_type and match_flag_type | Regex behavior depends heavily on option flags, so these types are first-class navigation targets. | ECMAScript grammar |
| Interpret captures, prefixes, suffixes, or matched subranges | match_results and sub_match | Result objects are the canonical route for understanding what the algorithms actually produced. | regex_search |
# Regex Building Blocks
| Family | Core destinations | Use it for |
|---|---|---|
| Pattern objects | basic_regex, regex_traits, syntax_option_type, grammar notes | Constructing and configuring a compiled regex together with its grammar and character-traits behavior. |
| Matching algorithms | regex_match, regex_search | Checking whole-string matches or searching for subranges inside larger text. |
| Replacement algorithm | regex_replace, match_flag_type | Producing rewritten output based on regex substitutions and replacement formatting rules. |
| Match result surfaces | match_results, sub_match | Accessing capture groups, matched spans, prefixes, suffixes, and related result metadata. |
| Iterators over matches and tokens | regex_iterator, regex_token_iterator | Walking repeated matches or token streams using iterator-based traversal. |
| Error and flag types | regex_error, error_type, match_flag_type, syntax_option_type | Interpreting failures and controlling pattern/matching behavior with the library's enum-style option surfaces. |
# Pattern Vs. Result Vs. Traversal
| Choose... | When your question is about... | Primary pages |
|---|---|---|
| Pattern definition | How to construct, store, and parameterize the regex itself. | basic_regex, syntax options, regex_traits |
| One-shot matching/searching | How to test or search input text against a compiled pattern. | regex_match, regex_search |
| Interpreting captures | What matched, what each capture group contains, and how prefixes/suffixes are reported. | match_results, sub_match |
| Repeated traversal | Walking every match or token across an input range. | regex_iterator, regex_token_iterator |
| Text rewriting | Producing a transformed output string from matches. | regex_replace |
# Version And Scope Notes
| Topic | Navigation note |
|---|---|
| C++11 | The standard regex library arrives as a single wave in C++11, so most navigation here is about choosing the right surface, not about major later standard-era branching. |
| Text storage vs pattern matching | Use string or text when the real issue is ownership, encoding, or text representation rather than matching algorithms. |
| Regex errors vs general diagnostics | Use regex_error for regex-specific failures, but go to diagnostics for broader exception and error-code navigation. |
| Formatting output | Use format or I/O when the main concern is output formatting rather than regex-driven replacement. |
# Practical Routes
I need to find patterns inside text
Start from `regex_search` when the match can occur anywhere inside a larger source string and you want capture groups back.
I need to rewrite matched text
Use the replace route when the end result is transformed output text rather than a boolean match result.
I need every match or token
Use the iterator route when processing many matches or delimiter-style token slices matters more than one-shot searching.