Skip to content

/cheatsheets/regex/regex-cheat-sheet

Regex cheat sheet: characters, quantifiers, groups and lookarounds

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · grep (GNU grep) 3.0updated 2026-10-20
tool
Regex
versions
PCRE2, Python re, JavaScript
os
any

The regular expression syntax shared by PCRE, Python, JavaScript, Java and .NET. Differences between engines are listed in regex features by engine.

Characters and classes

PatternMatches
.any character except a newline
\d \w \sdigit, word character [A-Za-z0-9_], whitespace
\D \W \Sthe opposites
[abc] [a-z] [^0-9]one of, a range, none of
\. \* \\a literal dot, star, backslash

Anchors

PatternMatches
^ $start and end of the string (of each line in multiline mode)
\b \Bword boundary, not a word boundary
\A \zstart and end of the string only (Python uses \Z for the end)

Quantifiers

PatternMeaning
* + ?0 or more, 1 or more, 0 or 1
{3} {2,} {2,5}exactly 3, at least 2, 2 to 5
*? +? ??lazy versions: as few as possible

Groups and lookarounds

PatternMeaning
(abc)capturing group, \1 refers back to it
(?:abc)non-capturing group
(?P<name>abc) / (?<name>abc)named group (Python / PCRE, JavaScript, .NET)
a|balternation
(?=x) (?!x)followed by x, not followed by x
(?<=x) (?<!x)preceded by x, not preceded by x

Common patterns, tested

bash
python - <<'EOF'
import re
tests = [
    (r"\b\d{4}-\d{2}-\d{2}\b", "released 2026-10-20, patched 2026-11-02"),
    (r"(?P<key>\w+)=(?P<value>[^&]*)", "q=regex&lang=en&page="),
    (r"#[0-9a-fA-F]{6}\b", "colors: #0e7c86 #FFF #a1b2c3"),
    (r"(?<=\$)\d+(?:\.\d{2})?", "total $42.50, tax $3"),
    (r"^\s*#.*$", "x = 1\n  # comment\ny = 2"),
    (r"\b(\w+)\s+\1\b", "this is is a test test"),
]
for pattern, text in tests:
    flags = re.M if pattern.startswith("^") else 0
    print(f"{pattern:32} -> {[m.group(0) for m in re.finditer(pattern, text, flags)]}")
EOF

Output

\b\d{4}-\d{2}-\d{2}\b            -> ['2026-10-20', '2026-11-02']
(?P<key>\w+)=(?P<value>[^&]*)    -> ['q=regex', 'lang=en', 'page=']
#[0-9a-fA-F]{6}\b                -> ['#0e7c86', '#a1b2c3']
(?<=\$)\d+(?:\.\d{2})?           -> ['42.50', '3']
^\s*#.*$                         -> ['  # comment']
\b(\w+)\s+\1\b                   -> ['is is', 'test test']

Sources