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.

Use this page when your question is about pattern matching with the standard regex library. Keep string as the hub for text storage and views, text for broader text-processing adjacency, and diagnostics for exception/error taxonomy beyond regex-specific failures.

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

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 withWhyCommon adjacent route
Validate that the entire input matches a patternregex_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 stringregex_searchThis is the canonical route for substring discovery and capture extraction from larger text.regex_match
Replace matching text with formatted outputregex_replaceThe replacement algorithm handles substitution syntax and formatting behavior directly.format
Walk every match across an input rangeregex_iteratorIterators provide repeated-match traversal without hand-written search loops.regex_token_iterator
Split or tokenize text around regex matches or submatchesregex_token_iteratorThis is the standard route for token-oriented regex iteration, including delimiter-style behavior.string
Choose grammar flavor or matching/replacement flagssyntax_option_type and match_flag_typeRegex behavior depends heavily on option flags, so these types are first-class navigation targets.ECMAScript grammar
Interpret captures, prefixes, suffixes, or matched subrangesmatch_results and sub_matchResult objects are the canonical route for understanding what the algorithms actually produced.regex_search

# Regex Building Blocks

FamilyCore destinationsUse it for
Pattern objectsbasic_regex, regex_traits, syntax_option_type, grammar notesConstructing and configuring a compiled regex together with its grammar and character-traits behavior.
Matching algorithmsregex_match, regex_searchChecking whole-string matches or searching for subranges inside larger text.
Replacement algorithmregex_replace, match_flag_typeProducing rewritten output based on regex substitutions and replacement formatting rules.
Match result surfacesmatch_results, sub_matchAccessing capture groups, matched spans, prefixes, suffixes, and related result metadata.
Iterators over matches and tokensregex_iterator, regex_token_iteratorWalking repeated matches or token streams using iterator-based traversal.
Error and flag typesregex_error, error_type, match_flag_type, syntax_option_typeInterpreting 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 definitionHow to construct, store, and parameterize the regex itself.basic_regex, syntax options, regex_traits
One-shot matching/searchingHow to test or search input text against a compiled pattern.regex_match, regex_search
Interpreting capturesWhat matched, what each capture group contains, and how prefixes/suffixes are reported.match_results, sub_match
Repeated traversalWalking every match or token across an input range.regex_iterator, regex_token_iterator
Text rewritingProducing a transformed output string from matches.regex_replace

# Version And Scope Notes

TopicNavigation note
C++11The 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 matchingUse string or text when the real issue is ownership, encoding, or text representation rather than matching algorithms.
Regex errors vs general diagnosticsUse regex_error for regex-specific failures, but go to diagnostics for broader exception and error-code navigation.
Formatting outputUse 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.