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.

Rust docs regex crate ↗

Separate literals from metacharacters

Rust

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

Fragment

src/main.rs
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

Rust

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

Fragment

src/main.rs
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

Rust

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

Fragment

src/main.rs
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

Rust

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

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

Group an expression without capturing it

Rust

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

Fragment

src/main.rs
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

Rust

| 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

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

Read input and line boundaries

Rust

^ 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

src/main.rs
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

Rust

By default, \d is Unicode-aware. Use [0-9] when the protocol requires ASCII digits.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
\d
[0-9]

Name a capture

Rust

regex 1.9+ accepts (?<name>...) as well as (?P<name>...); retrieve named groups with Captures::name.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
(?<word>[A-Za-z]+)

Recognize capture backreferences

Rust

The regex crate does not support pattern backreferences. Extract captures and compare in Rust, or explicitly choose a different engine.

Check lookaround support before using it

Rust

The regex crate intentionally omits lookaround. Choose matching/capture plus application logic when it fits.

Select case, line and dot behavior

Rust

Inline flags include i/m/s and x for insignificant pattern whitespace/comments outside relevant constructs.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
(?imsx) pattern

Compile or construct a pattern

Rust

Regex::new returns Result. The regex crate is an optional Cargo dependency; compile reusable patterns outside hot loops.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let re = regex::Regex::new(r"[0-9]+")?;

Ask whether any substring matches

Rust

is_match finds existence without constructing capture results.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let found = re.is_match(input);

Match an entire input

Rust

Use explicit text anchors when matching an entire haystack.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let re = regex::Regex::new(r"\A[A-Z]{2}[0-9]{3}\z")?;
let valid = re.is_match(input);

Read captures and missing groups

Rust

captures returns an Option. Capture groups can themselves be absent; use name/get rather than indexing when that is possible.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
if let Some(caps) = re.captures(input) {
    if let Some(name) = caps.name("name") {
        println!("{}", name.as_str());
    }
}

Iterate non-overlapping matches

Rust

find_iter yields matches with byte offsets into the UTF-8 input.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
for matched in re.find_iter(input) {
    println!("{}", matched.as_str());
}

Distinguish replacement syntax from literal text

Rust

replace_all interprets capture references in replacement strings. NoExpand requests a literal replacement.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let result = re.replace_all(input, regex::NoExpand("$literal"));

Quote dynamic input as literal pattern text

Rust

regex::escape quotes metacharacters before building a pattern from literal input.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let re = regex::Regex::new(&regex::escape(user_text))?;

Choose Unicode properties deliberately

Rust

Unicode support is enabled by default for the regex crate and can be controlled through features/flags.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
\p{L}+

Distinguish dot from an arbitrary text parser

Rust

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

Fragment

src/main.rs
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

Rust

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

Fragment

src/main.rs
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

Rust

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

Fragment

src/main.rs
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

Rust

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

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

Choose an engine and input budget that fit

Rust

The regex crate gives O(m×n) search bounds in pattern-size m and haystack-size n. Iterating all matches can have larger worst-case costs; bound inputs and result retention.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Use is_match when only existence is needed.

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

Rust

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

src/main.rs
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

Rust

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

src/main.rs
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