Skip to Content
Regular Expression Syntax Cheat Sheet

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.

SyntaxDescriptionExample patternMatchesNon-matches
^Start of a line^rrabbit, raccoonparrot, ferret
$End of a linet$rabbit, foottrap, star
\AStart of a string\Arrabbit (at start)parrot, inner matches
\ZEnd of a stringt\Zrabbit (at end)trap, inner matches
\bWord boundary (start or end of a word)\bfox\bthe fox, red foxfoxtrot, foxskin
\BNot a word boundary\Bee\Btrees, beefbee, tree

๐Ÿ”ค Character Types

Match specific categories of characters.

SyntaxDescriptionExample patternMatchesNon-matches
.Any character except newlinec.eclean, cheapacert, cent
\dDigit (0-9)\d6060, 2btwo, words
\DNon-digit\DThe, cats, ate52, 10032
\wWord character [a-zA-Z0-9_]\wee\wtrees, bee4bee, eels
\WNon-word character\Wbat\Wbat, ,bat.wombat, bat53
\sWhitespace\sfox\sfoxfox., foxfur
\SNon-whitespace\See\Strees, beeftall tree, bee stung
\xEscaped metacharacter (e.g. \.)\.. (literal dot)dot

๐Ÿงฑ Character Classes

Define a set of characters or ranges to match.

SyntaxDescriptionExample patternMatchesNon-matches
[xy]Match any character in setgr[ea]ygray, greygreen, greek
[x-y]Match a character in range[a-e]a, b, cf, z
[^xy]Not any character in setgr[^ea]ygruygray, grey
[\^-]Match a literal ^ or -4[\^.-]\d4^3, 4-244, 23

๐Ÿ” Repetition

Match characters repeated a number of times.

SyntaxDescriptionExampleMatchesNon-matches
x*0 or morear*oao, arroart, arugula
x+1 or morere+ree, treeruin, root
x?0 or 1ro?ara, roaroot, roar
x{m}Exactly me{2}deer, seesred, enter
x{m,}At least m2{3,}2224, 222222422, 123
x{m,n}Between m and n12{1,3}3123, 122315335, 1222223
x+?Lazy match (as few as possible)re+?re, reereel, reeee

๐ŸŽฏ Groups, Alternation & Backreferences

Group patterns, name them, or refer back to them.

SyntaxDescriptionExampleMatchesNon-matches
(x)Capturing group(iss)+Mississippimist, persist
(?:x)Non-capturing group(?:ab)(cd)abcd (captures cd)acbd
(?<name>x)Named capture(?<a>\d)(?<b>\d)25 => a=2, b=5x5, ab
`(xy)`Alternation (or)`(reba)`
\1Backreference to group 1(b)(\w*)\1bob, bribbear, bib
\k<name>Named backreference(?<x>5)\d*\k<x>51245, 55523, 51

๐Ÿ” Lookaround

Match based on whatโ€™s before or after, without capturing it.

SyntaxDescriptionExampleMatchesNon-matches
(?=x)Positive lookaheadan(?=an)banana, ananasband
(?!x)Negative lookaheadai(?!n)fail, bailfaint, train
(?<=x)Positive lookbehind(?<=tr)atrail, trackstreak, bear
(?<!x)Negative lookbehind(?<!tr)abear, staretrail, strain

๐Ÿ› ๏ธ Modifiers and Literals

Control the behavior of the pattern engine.

SyntaxDescriptionExampleMatchesNon-matches
\Q...\EEscape entire literal block\Q{hello}\E{hello}hello
(?i)...(?-i)Case-insensitive within group(?i)te(?-i)Teach, Testtrench
(?x)...(?-x)Ignore whitespace inside regex(?x)t a p(?-x)tap, tapdancet a p, ta p
(?s)...(?-s)Dot matches newline (DOTALL)(?s)first.*third(?-s)includes \nmultiline mismatch
(?m)^...$Multiline mode for ^ and $^eat and sleep$full line matchesmid-line only

๐ŸŒ Unicode & Graphemes

Work with extended character sets, including emoji and accents.

SyntaxDescriptionExampleMatchesNon-matches
\XMatch one grapheme cluster\X\X on ๐Ÿ‡บ๐Ÿ‡ธfull emoji flaghalf emoji
\uXXXXUnicode character by code point\u00e8 or \u0065\u0300รจe

๐Ÿ“š Resources

Last updated on