Regular Expression Reference

Character Classes

PatternMatches / MeaningExample
.Any character except newlinehe.llo
\dAny digit (0-9)\d{3}
\DAny non-digit\D{3}
\wAny word character (a-z, A-Z, 0-9, _)\w{3}
\WAny non-word character\W{3}
\sAny whitespace character\s{3}
\SAny non-whitespace character\S{3}
[abc]Any character in the set[abc]{3}
[^abc]Any character not in the set[^abc]{3}
[a-z]Any lowercase letter[a-z]{3}
[A-Z]Any uppercase letter[A-Z]{3}
[0-9]Any digit[0-9]{3}
[a-zA-Z]Any letter[a-zA-Z]{3}
[^a-zA-Z]Any non-letter[^a-zA-Z]{3}
[0-9a-fA-F]Any hexadecimal digit[0-9a-fA-F]{3}

Anchors

PatternMatches / MeaningExample
^Start of string^hello
$End of stringhello$
\bWord boundary\bhello\b
\BNon-word boundary\Bhello\B
\AStart of string (multiline)\Ahello
\ZEnd of string (multiline)hello\Z
\zEnd of string (strict)hello\z

Quantifiers

PatternMatches / MeaningExample
*Zero or more of the preceding elementhe*llo
+One or more of the preceding elementhe+llo
?Zero or one of the preceding elementhe?llo
{n}Exactly n occurrences of the preceding elementhe{2}llo
{n,}n or more occurrences of the preceding elementhe{2,}llo
{n,m}Between n and m occurrences of the preceding elementhe{2,4}llo
*?Zero or more of the preceding element (lazy)he*?llo
+?One or more of the preceding element (lazy)he+?llo
??Zero or one of the preceding element (lazy)he??llo
{n,m}?Between n and m occurrences (lazy)he{2,4}?llo

Groups & Backreferences

PatternMatches / MeaningExample
(group)Grouping(he){2}llo
(?:non-capturing)Non-capturing group(?:he){2}llo
\1Backreference to the first group(he)\1llo
\2Backreference to the second group(he)(wo)\2llo
(?)Named group(?he)\gllo
\k<name>Backreference by name(?he)\kllo
(?:...)Non-capturing group(?:he){2}llo
(?=...)Positive lookaheadhe(?=llo)
(?!...)Negative lookaheadhe(?!llo)
(?<=...)Positive lookbehind(?<=he)llo
(?<!...)Negative lookbehind(?

Lookarounds

PatternMatches / MeaningExample
(?=)Positive lookaheadhe(?=llo)
(?!)Negative lookaheadhe(?!llo)
(?<=)Positive lookbehind(?<=he)llo
(?Negative lookbehind(?
(?=(?=...))Nested lookaheadhe(?=(?=llo))
(?=(?!...))Nested negative lookaheadhe(?=(?!llo))
(?<=...)(?=...)Combined lookbehind and lookahead(?<=he)(?=llo)

Flags

PatternMatches / MeaningExample
gGlobal searchhe/g
iCase-insensitive searchhe/i
mMultiline searchhe/m
sDot matches newlinehe/s
xIgnore whitespace and commentshe/x
uUnicode modehe/u
ySticky searchhe/y
giGlobal and case-insensitivehe/gi
gmGlobal and multilinehe/gm

Common Patterns

PatternMatches / MeaningExample
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\bEmail addressexample@example.com
https?://[^\s/$.?#].[^\s]*URLhttp://example.com
\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\bIPv4 address192.168.1.1
#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})Hex color#ff0000
\d{4}-\d{2}-\d{2}ISO date2023-10-05
\d{2}\/\d{2}\/\d{4}MM/DD/YYYY date10/05/2023
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}IPv4 address (simplified)192.168.1.1
^0x[0-9a-fA-F]+$Hexadecimal number0xFF
^\d{3}-\d{2}-\d{4}$US SSN123-45-6789
^\+?[1-9]\d{0,15}$Phone number+1234567890
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$Basic emailuser@domain.com
^([0-9]{4})-([0-9]{2})-([0-9]{2})$ISO date with groups2023-10-05
^([0-9]{1,3}\.){3}[0-9]{1,3}$IPv4 with groups192.168.1.1
^#([A-Fa-f0-9]{3}|[A-Fa-f0-9]{6})$Hex color with groups#ff0000
^https?:\/\/[^\s\/$.?#].[^\s]*$URL with groupshttp://example.com