/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
| Pattern | Matches |
|---|---|
| . | any character except a newline |
| \d \w \s | digit, word character [A-Za-z0-9_], whitespace |
| \D \W \S | the opposites |
| [abc] [a-z] [^0-9] | one of, a range, none of |
| \. \* \\ | a literal dot, star, backslash |
Anchors
| Pattern | Matches |
|---|---|
| ^ $ | start and end of the string (of each line in multiline mode) |
| \b \B | word boundary, not a word boundary |
| \A \z | start and end of the string only (Python uses \Z for the end) |
Quantifiers
| Pattern | Meaning |
|---|---|
| * + ? | 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
| Pattern | Meaning |
|---|---|
| (abc) | capturing group, \1 refers back to it |
| (?:abc) | non-capturing group |
| (?P<name>abc) / (?<name>abc) | named group (Python / PCRE, JavaScript, .NET) |
| a|b | alternation |
| (?=x) (?!x) | followed by x, not followed by x |
| (?<=x) (?<!x) | preceded by x, not preceded by x |
Common patterns, tested
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)]}")
EOFOutput
\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
Related pages
compat · regexRegex features by engine: JavaScript, Python, PCRE and POSIX EREWhich regex features work where: lookbehind, named groups, \p{L}, atomic groups and possessive quantifiers in JavaScript, Python re, PCRE and POSIX ERE, tested.flags · regexJavaScript regex flags: g, i, m, s, u, v, y and dWhat each JavaScript RegExp flag does: g finds all matches, i ignores case, m works per line, s lets . match newlines, u and v add Unicode, plus y and d.glossary · regexGreedy vs lazy quantifiers in regexA greedy quantifier such as .* matches as much as it can and backtracks only when needed; a lazy one such as .*? matches as little as possible. Examples.