Regex Cheatsheet
Regex syntax with examples, patterns to adapt, and what differs between engines.
By BoltQuickTools team · Updated November 8, 2026
Syntax here is common to most engines unless noted. Try patterns in the regex tester, which uses your browser's JavaScript engine, so patterns specific to Python or PCRE may behave differently there. For a conceptual walkthrough, see the regex guide.
Character Classes
| Pattern | Matches | Example |
|---|---|---|
| . | Any character except a line break (unless the s flag is on) | a.c matches "abc", "aXc" |
| \d | Digit | \d{3} matches "123" |
| \D | Non-digit | \D+ matches "abc!" |
| \w | Word character: letter, digit, underscore | \w+ matches "hello_123" |
| \W | Non-word character | \W matches a space or "!" |
| \s | Whitespace, including tabs and line breaks | \s+ matches " " |
| \S | Non-whitespace | \S+ matches "word" |
| [abc] | Any one of a, b, c | [aeiou] matches a vowel |
| [^abc] | Any one except a, b, c | [^0-9] matches a non-digit |
| [a-z] | Range a through z | [A-Za-z0-9] alphanumeric |
| \t \n \r | Tab, line feed, carriage return | \r?\n matches either line ending |
| \xHH \uHHHH | Character by hex code (\u support varies) | \x41 matches "A" |
| \p{L} \p{Lu} | Unicode property: any letter, uppercase letter (see engine table) | \p{L}+ matches "café" |
\d and \w are ASCII-only in JavaScript, Go and Java by default. In Python 3 string patterns and in .NET they also match non-ASCII digits and letters. Use [0-9] when you need exactly the ASCII digits.
Quantifiers
| Pattern | Matches | Notes |
|---|---|---|
| * | 0 or more | Greedy: takes as much as it can, then gives back |
| + | 1 or more | Greedy |
| ? | 0 or 1 | After another quantifier, makes it lazy |
| {n} | Exactly n | \d{3} |
| {n,} | n or more | \d{3,} |
| {n,m} | Between n and m | \d{3,5} |
| *? +? ?? {n,m}? | Lazy versions: as few as possible | Changes which match is found first. Lazy patterns still backtrack. |
| *+ ++ ?+ | Possessive: never give back | See engine table |
Anchors and Boundaries
| Pattern | Matches | Example |
|---|---|---|
| ^ | Start of string, or of each line with the m flag | ^hello |
| $ | End of string, or of each line with the m flag. In Python, PCRE and Java it can also match just before a final newline. | world$ |
| \A \z | Absolute start and end of string (not JavaScript; Python 3 used \Z for the end before adding \z) | \Aid=\d+\z |
| \b | Word boundary | \bcat\b matches "cat" but not inside "category" |
| \B | Not a word boundary | \Bcat matches the "cat" in "scattered" |
Groups and References
| Pattern | Matches | Example |
|---|---|---|
| (abc) | Capturing group | (\d{3})-(\d{4}) captures two parts |
| (?:abc) | Group without capturing | (?:ab)+ matches "abab" |
| (?<name>abc) | Named group. Python's re uses (?P<name>abc) | Refer back with \k<name> |
| \1, \2 | Backreference to a group | (a)\1 matches "aa" |
| | | Alternation. Lowest precedence, so group it to limit its reach. | ^(?:cat|dog)$ |
| (?>abc) | Atomic group: no backtracking into it | See engine table |
| $1 or \1 | In replacement strings. JavaScript and .NET use $1; Python uses \1 | Swap: (\w+) (\w+) to $2 $1 |
Lookarounds
| Pattern | Matches | Example |
|---|---|---|
| (?=abc) | Followed by abc | \d+(?=px) gets "100" from "100px" |
| (?!abc) | Not followed by abc | \d+(?!\d|px) skips "100px" |
| (?<=abc) | Preceded by abc | (?<=\$)\d+ gets "20" from "$20" |
| (?<!abc) | Not preceded by abc | (?<!\$)\b\d+ skips "$20" |
Lookarounds consume nothing, so a greedy quantifier can step back to satisfy one. \d+(?!px) on "100px" matches "10", because the next character is then "0", which is not "px". Adding \d to the lookahead, as in the table, stops that.
Flags
| Flag | Effect |
|---|---|
| i | Case-insensitive |
| g | Find all matches, not just the first (JavaScript, PCRE tools). Python uses findall or finditer instead. |
| m | ^ and $ match at line boundaries |
| s | . also matches line breaks |
| u | Unicode mode (JavaScript); required for \p{...} |
| v | JavaScript's newer Unicode mode, adding set operations in classes |
| y | Sticky: match only at lastIndex (JavaScript) |
| d | Include capture start and end positions (JavaScript) |
| x | Ignore whitespace in the pattern and allow comments (not JavaScript) |
Engine Differences
| Feature | JavaScript | Python re | PCRE | Java | .NET | Go |
|---|---|---|---|---|---|---|
| Lookahead | Yes | Yes | Yes | Yes | Yes | No |
| Lookbehind | Yes, any length | Fixed width | Limited length | Bounded length | Yes, any length | No |
| Backreferences | Yes | Yes | Yes | Yes | Yes | No |
| Atomic groups | No | 3.11+ | Yes | Yes | Yes | No |
| Possessive | No | 3.11+ | Yes | Yes | No | No |
| \p{...} | With u or v | No (third-party regex module has it) | Yes | Yes | Yes | Yes |
| Named groups | (?<n>) | (?P<n>) | Both | (?<n>) | (?<n>) | (?P<n>), (?<n>) since 1.22 |
Go's RE2 engine gives up backreferences and lookaround in exchange for matching time that grows linearly with input, so it cannot suffer catastrophic backtracking.
Patterns to Adapt
These check shape, not truth. A pattern can say a date looks right, but not that it exists.
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$^https?:\/\/[^\s/$.?#].[^\s]*$^\+[1-9]\d{1,14}$^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$^#(?:[a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$^(?:[a-zA-Z0-9+\/]{4})*(?:[a-zA-Z0-9+\/]{2}==|[a-zA-Z0-9+\/]{3}=)?$^(?:(?:25[0-5]|2[0-4]\d|1?\d?\d)\.){3}(?:25[0-5]|2[0-4]\d|1?\d?\d)$^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$^[a-z0-9]+(-[a-z0-9]+)*$(?<=\$)\d+(\.\d{2})?\b[A-Z][a-z0-9]*(?:[A-Z][a-z0-9]*)+\b\b[a-z]+(?:[A-Z][a-z0-9]*)+\b^\s+|\s+$^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{12,}$Common Gotchas
- Greedy by default. On
<a><b>,<.*>matches the whole string.<.*?>matches<a>, and<[^>]*>does the same without backtracking. - Escape metacharacters.
. * + ? ( ) [ ] { } | \ ^ $need a backslash to match literally. The regex escape tool does it for you. - Character classes have their own rules. Generally only
] \ ^ -are special inside[ ], and^and-only in certain positions. JavaScript'svflag reserves more. - Double escaping in strings. A pattern inside a normal string literal needs
\\d. Use raw strings or regex literals where available. - The g flag remembers. In JavaScript,
re.test()withgoryadvanceslastIndex, so calling it twice on the same text can give different results. - Catastrophic backtracking. Patterns like
(a+)+bcan take exponential time on input that almost matches. Avoid nested quantifiers over the same characters, and never run untrusted patterns on a backtracking engine without a timeout. - Emulating atomic groups in JavaScript.
(?=(a+))\1captures in a lookahead, then consumes the capture, so it cannot give characters back. - Regex cannot parse nested structures. For HTML, JSON and similar formats, use a parser.
Last updated