Regular Expressions Cheatsheet

A quick reference for pattern matching — Python · JavaScript · Go · PHP · Java · Ruby

Anchors

^Start of string / line
$End of string / line
\bWord boundary
\BNot a word boundary
\AStart of string (no multiline)
\ZEnd of string (no multiline)
\zAbsolute end of string
\GStart of current search (pos)
\KReset match start (keep left)
(?m)^Start of each line (multiline)
(?m)$End of each line (multiline)

Character Classes

.Any char except newline
\wWord char [a-zA-Z0-9_]
\WNon-word character
\dDigit [0-9]
\DNon-digit
\sWhitespace [ \t\n\r\f]
\SNon-whitespace
[abc]Any of a, b, or c
[^abc]Not a, b, or c
[a-z]Range a to z
[a-zA-Z0-9]Alphanumeric
\p{L}Unicode letter (PCRE/Java)
\p{N}Unicode number

Quantifiers

*0 or more (greedy)
+1 or more (greedy)
?0 or 1 (greedy)
{n}Exactly n times
{n,}n or more times
{n,m}Between n and m times
*?0 or more (lazy)
+?1 or more (lazy)
??0 or 1 (lazy)
*+0 or more (possessive)
++1 or more (possessive)

Groups

(abc)Capture group
(?:abc)Non-capturing group
(?<name>abc)Named capture group
(?P<name>abc)Named group (Python)
\1Backreference to group 1
(?P=name)Named backreference (Python)
(?>abc)Atomic group (no backtrack)
a|bAlternation: a or b

Lookarounds

(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind
Lookahead example
\d+(?=px) → matches digits before "px"
Lookbehind example
(?<=\$)\d+ → matches digits after "$"

Flags / Modifiers

iCase insensitive
gGlobal — find all matches
mMultiline — ^ and $ per line
sDotall — . matches \n too
xVerbose — ignore whitespace
uUnicode mode
(?ims)Multiple inline flags
(?-i)Turn off flag inline

Escape Sequences

\nNewline (LF, U+000A)
\rCarriage return (CR, U+000D)
\tHorizontal tab (U+0009)
\vVertical tab (U+000B)
\fForm feed (U+000C)
\aBell (U+0007)
\eEscape (U+001B)
\0Null character (U+0000)
\xFFHex byte (e.g. \x41 = A)
\uFFFFUnicode code point (4 hex)
\u{1F600}Unicode (JS with /u flag)
\cXControl char (e.g. \cM = CR)
\\Literal backslash
\. \* \+ \? \^ \$Literal meta characters
\( \) \[ \] \{ \}Literal brackets
\| \/Literal pipe / slash

Replacement / Substitution

$1 or \1Insert capture group 1
${1}Group 1 (unambiguous)
$&Entire match
$`String before match
$'String after match
$$Literal $ in replacement
$<name>Named group (JS)
\g<name>Named group (Python)
\g<1>Group by number (Python)
${name}Named group (.NET / JS)
\U\1Uppercase group (Perl/PCRE)
\L\1Lowercase group (Perl/PCRE)
\EEnd \U or \L

Common Patterns

[\w.+-]+@[\w-]+\.[a-z]{2,}Email
https?://[\w./%-]+(\?[\w=&%-]+)?URL
\d{1,3}(\.\d{1,3}){3}IPv4 address
([0-9a-f]{2}:){5}[0-9a-f]{2}MAC address
^\+?[\d\s()\-]{7,15}$Phone number
^#?[0-9a-fA-F]{6}$Hex color (#RRGGBB)
^#?([0-9a-fA-F]{3}){1,2}$Hex color (3 or 6 digits)
\d{4}-\d{2}-\d{2}Date YYYY-MM-DD
\d{2}[\/.-]\d{2}[\/.-]\d{4}Date DD/MM/YYYY
\d{2}:\d{2}(:\d{2})?Time HH:MM[:SS]
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$Strong password
^[a-zA-Z0-9_]{3,20}$Username
^\d{4}[\s-]?\d{4}[\s-]?\d{4}[\s-]?\d{4}$Credit card number
^[a-z][a-z0-9\-]{1,61}[a-z0-9]$Domain name
[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}UUID / GUID
<[^>]+>HTML tag (basic)
\/\*[\s\S]*?\*\//* Block comment */
^\s+|\s+$Leading/trailing whitespace
\b\d+(\.\d+)?\bInteger or float
^[01]?\d|2[0-3]):[0-5]\d$24-hour time HH:MM

JavaScript

/pattern/flagsRegex literal
new RegExp(str, flags)Dynamic regex
re.test(str)Returns true/false
str.match(re)Array of matches or null
str.matchAll(re)Iterator of all matches
str.search(re)Index of first match or -1
str.replace(re, '$1')Replace with string or fn
str.replaceAll(re, s)Replace all (needs g flag)
str.split(re)Split by pattern

Python (re module)

re.match(p, s)Match at start of string
re.search(p, s)Search anywhere in string
re.findall(p, s)List of all matches
re.finditer(p, s)Iterator of match objects
re.sub(p, repl, s)Replace matches
re.subn(p, repl, s)Replace + count
re.split(p, s)Split by pattern
re.compile(p, re.I)Compile with flags
m.group(1)Get capture group value
m.groupdict()Named groups as dict
m.span()Start and end positions

POSIX Classes

[:alpha:]Letters [a-zA-Z]
[:digit:]Digits [0-9]
[:alnum:]Letters and digits
[:space:]Whitespace
[:upper:]Uppercase letters
[:lower:]Lowercase letters
[:punct:]Punctuation
[:print:]Printable characters
[:xdigit:]Hex digits [0-9a-fA-F]