Regular Expression Syntax Cheat Sheet
Regular expressions (regex or regexp) are patterns used to match character combinations in strings.
Anchors
Anchors match positions within text, not actual characters.
| Syntax | Description | Example pattern | Matches | Non-matches |
|---|---|---|---|---|
^ | Start of a line | ^r | rabbit, raccoon | parrot, ferret |
$ | End of a line | t$ | rabbit, foot | trap, star |
\A | Start of a string | \Ar | rabbit (at start) | parrot, inner matches |
\Z | End of a string | t\Z | rabbit (at end) | trap, inner matches |
\b | Word boundary (start or end of a word) | \bfox\b | the fox, red fox | foxtrot, foxskin |
\B | Not a word boundary | \Bee\B | trees, beef | bee, tree |
Character Types
Match specific categories of characters.
| Syntax | Description | Example pattern | Matches | Non-matches |
|---|---|---|---|---|
. | Any character except newline | c.e | clean, cheap | acert, cent |
\d | Digit (0-9) | \d | 6060, 2b | two, words |
\D | Non-digit | \D | The, cats, ate | 52, 10032 |
\w | Word character [a-zA-Z0-9_] | \wee\w | trees, bee4 | bee, eels |
\W | Non-word character | \Wbat\W | bat, ,bat. | wombat, bat53 |
\s | Whitespace | \sfox\s | fox | fox., foxfur |
\S | Non-whitespace | \See\S | trees, beef | tall tree, bee stung |
\x | Escaped metacharacter (e.g. \.) | \. | . (literal dot) | dot |
Character Classes
Define a set of characters or ranges to match.
| Syntax | Description | Example pattern | Matches | Non-matches |
|---|---|---|---|---|
[xy] | Match any character in set | gr[ea]y | gray, grey | green, greek |
[x-y] | Match a character in range | [a-e] | a, b, c | f, z |
[^xy] | Not any character in set | gr[^ea]y | gruy | gray, grey |
[\^-] | Match a literal ^ or - | 4[\^.-]\d | 4^3, 4-2 | 44, 23 |
Repetition
Match characters repeated a number of times.
| Syntax | Description | Example | Matches | Non-matches |
|---|---|---|---|---|
x* | 0 or more | ar*o | ao, arro | art, arugula |
x+ | 1 or more | re+ | ree, tree | ruin, root |
x? | 0 or 1 | ro?a | ra, roa | root, roar |
x{m} | Exactly m | e{2} | deer, sees | red, enter |
x{m,} | At least m | 2{3,} | 2224, 2222224 | 22, 123 |
x{m,n} | Between m and n | 12{1,3}3 | 123, 1223 | 15335, 1222223 |
x+? | Lazy match (as few as possible) | re+? | re, ree | reel, reeee |
Groups, Alternation & Backreferences
Group patterns, name them, or refer back to them.
| Syntax | Description | Example | Matches | Non-matches |
|---|---|---|---|---|
(x) | Capturing group | (iss)+ | Mississippi | mist, persist |
(?:x) | Non-capturing group | (?:ab)(cd) | abcd (captures cd) | acbd |
(?<name>x) | Named capture | (?<a>\d)(?<b>\d) | 25 => a=2, b=5 | x5, ab |
| `(x | y)` | Alternation (or) | `(re | ba)` |
\1 | Backreference to group 1 | (b)(\w*)\1 | bob, brib | bear, bib |
\k<name> | Named backreference | (?<x>5)\d*\k<x> | 51245, 55 | 523, 51 |
Lookaround
Match based on what’s before or after, without capturing it.
| Syntax | Description | Example | Matches | Non-matches |
|---|---|---|---|---|
(?=x) | Positive lookahead | an(?=an) | banana, ananas | band |
(?!x) | Negative lookahead | ai(?!n) | fail, bail | faint, train |
(?<=x) | Positive lookbehind | (?<=tr)a | trail, track | streak, bear |
(?<!x) | Negative lookbehind | (?<!tr)a | bear, stare | trail, strain |
Modifiers and Literals
Control the behavior of the pattern engine.
| Syntax | Description | Example | Matches | Non-matches |
|---|---|---|---|---|
\Q...\E | Escape entire literal block | \Q{hello}\E | {hello} | hello |
(?i)...(?-i) | Case-insensitive within group | (?i)te(?-i) | Teach, Test | trench |
(?x)...(?-x) | Ignore whitespace inside regex | (?x)t a p(?-x) | tap, tapdance | t a p, ta p |
(?s)...(?-s) | Dot matches newline (DOTALL) | (?s)first.*third(?-s) | includes \n | multiline mismatch |
(?m)^...$ | Multiline mode for ^ and $ | ^eat and sleep$ | full line matches | mid-line only |
Unicode & Graphemes
Work with extended character sets, including emoji and accents.
| Syntax | Description | Example | Matches | Non-matches |
|---|---|---|---|---|
\X | Match one grapheme cluster | \X\X on 🇺🇸 | full emoji flag | half emoji |
\uXXXX | Unicode character by code point | \u00e8 or \u0065\u0300 | è | e |
Resources
Last updated on