Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Patterns & regex

TypeScript uses JavaScript RegExp; Go uses regexp; Rust uses the optional regex crate; C++ uses std::regex with its default ECMAScript grammar. Pattern rows show regex text; API rows show host-language escaping.

Separate literals from metacharacters

C++

Ordinary characters match themselves. Metacharacters have structural meaning; escape one when its literal form is intended.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cat       matches the sequence cat
a.b       . is a wildcard
a\.b      matches the literal a.b

Choose a character from a class

C++

A bracket class matches one member. A leading ^ inside the class negates it; a hyphen can specify a range.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
[abc]     one of a, b, c
[A-Z]     one ASCII uppercase letter
[^0-9]    one character outside ASCII digits

Repeat the preceding atom

C++

Quantifiers apply to the preceding atom or group. They count matched units under the engine’s character model, not user-perceived grapheme clusters.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
a?        zero or one
a*        zero or more
a+        one or more
a{2,4}    from two to four

Greedy and lazy repetition

C++

A greedy quantifier prefers more input when a match is possible. Appending ? makes it prefer less. Lazy repetition is not a substitute for choosing a correct grammar.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
<.*>      greedy
<.*?>     lazy

Group an expression without capturing it

C++

Parentheses group alternatives or repetition and normally capture their matched text. (?:...) groups without assigning a capture number.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
(ab)+       captures the repeated group
(?:ab)+     groups without capturing

Choose among alternatives

C++

| separates alternatives; group them when only part of a pattern should vary. Default alternatives are tried in order, under each engine’s matching policy.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cat|dog
(?:cat|dog)s?

Read input and line boundaries

C++

^ and $ are boundary assertions, not matched characters. Their line behavior depends on multiline flags. Use the engine’s full-match contract when validating an entire input.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
^name$
# Confirm multiline/end-boundary behavior for the named engine.

Know whether digit shorthand includes Unicode

C++

Character-class behavior follows the selected grammar and regex traits/locale. Use explicit ASCII ranges for an ASCII protocol.

Fragment

Do not assume narrow std::regex is a Unicode-property engine.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
[0-9]

Name a capture

C++

The default std::regex ECMAScript grammar has numbered captures, not named capture syntax.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
([A-Za-z]+)

Recognize capture backreferences

C++

Default ECMAScript grammar supports numeric pattern backreferences. Engine performance and capture behavior remain implementation-specific.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
([A-Za-z]+)\s+\1

Check lookaround support before using it

C++

Default ECMAScript grammar supports lookahead, but not JavaScript’s newer lookbehind syntax.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cat(?=s)
cat(?!s)

Select case, line and dot behavior

C++

Flags are supplied when constructing the regex. icase and C++17 multiline are distinct from dot-all behavior, which has no equivalent standard flag. multiline arrived late in some standard libraries, so check ^/$ behavior on yours before relying on it.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
std::regex re("pattern",
    std::regex::ECMAScript | std::regex::icase);

Compile or construct a pattern

C++

Construction can throw regex_error. Raw string literals avoid doubling regex backslashes.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <regex>
std::regex re(R"([0-9]+)");

Ask whether any substring matches

C++

regex_search searches within the input. regex_match requires the entire input to match.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
bool found = std::regex_search(input, re);

Match an entire input

C++

regex_match applies the pattern to the complete input range.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
bool valid = std::regex_match(input, re);

Read captures and missing groups

C++

smatch holds sub_match values whose matched flag distinguishes an unmatched group. Its iterators refer into the searched input.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
std::smatch match;
if (std::regex_search(input, match, re)) {
    std::cout << match[0].str();
}

Iterate non-overlapping matches

C++

sregex_iterator traverses non-overlapping matches while retaining references to the input and regex.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
for (std::sregex_iterator it(input.begin(), input.end(), re), end;
     it != end; ++it) {
    std::cout << it->str() << '\n';
}

Distinguish replacement syntax from literal text

C++

regex_replace interprets the selected format grammar. Escape/transform replacement data deliberately when it contains dollar substitutions.

Fragment

$1 means the first capture under the default format grammar.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
auto result = std::regex_replace(input, re, "$1");

Quote dynamic input as literal pattern text

C++

std::regex has no standard escape helper. For literal substring search, prefer string::find; use a reviewed grammar-specific quoting routine only when regex composition is required.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
auto position = input.find(userText);

Choose Unicode properties deliberately

C++

Standard narrow std::regex does not provide the same Unicode property syntax. Choose an explicitly Unicode-aware library such as ICU when that is a requirement.

Distinguish dot from an arbitrary text parser

C++

Dot’s newline behavior depends on flags and engine semantics. A wildcard pattern does not parse nested or escaped structured data.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
a.b
# Use the engine’s dot-all option when newline matching is intended.

Track pattern, string and transport escaping separately

C++

A regex pattern embedded in a host string and then JSON has multiple encoding layers. Decode each layer before interpreting the next.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Regex intent:        \d+
JSON string value:   "\\d+"
Decoded regex text:  \d+

Check what a word boundary means

C++

\b is defined relative to the engine’s word-character class; Unicode behavior differs. It does not implement natural-language word segmentation.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
\bcat\b
# State the engine/Unicode mode when word boundaries matter.

Handle patterns that can match an empty string

C++

A pattern such as a* can match without consuming input. Use a documented iterator API rather than writing a loop that retries the same empty match forever.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
a*
^
(?:)

Choose an engine and input budget that fit

C++

std::regex does not promise linear-time behavior for all patterns. Engine/library choice, pattern structure and input limits matter.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Do not assume optimize removes pathological behavior.

Use a format-aware parser when the data has a grammar

C++

Regex is useful for bounded token recognition and extraction. JSON, CSV, HTML and nested-language parsing need their format rules, escaping and error handling.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
CSV: a quoted field can contain a comma or newline
JSON: strings can contain escaped delimiters

Separate normalization from matching

C++

Unicode normalization and case policies can change which strings are considered equivalent. Normalize only under an explicit domain contract; matching alone does not establish semantic identity.

Fragment

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
NFC/NFD: canonical normalization forms
Case folding: a separate language/Unicode policy