ByteTech LabsByte Utility
← All tools

Regex Cheat Sheet

Quick reference for regular expression syntax

Anchors

SyntaxDescriptionExample
^Start of string (or line with m flag)^Hello
$End of string (or line with m flag)world$
\bWord boundary\bcat\b
\BNon-word boundary\Bcat\B

Character Classes

SyntaxDescriptionExample
\dAny digit [0-9]\d+
\DNon-digit\D+
\wWord character [a-zA-Z0-9_]\w+
\WNon-word character\W+
\sWhitespace (space, tab, newline)\s+
\SNon-whitespace\S+
.Any character except newlinea.b

Quantifiers

SyntaxDescriptionExample
*0 or more (greedy)a*
+1 or more (greedy)a+
?0 or 1 (optional)colou?r
{n}Exactly n times\d{4}
{n,}n or more times\d{2,}
{n,m}Between n and m times\d{2,4}
*?0 or more (lazy/non-greedy)a.*?b
+?1 or more (lazy)a.+?b

Groups

SyntaxDescriptionExample
(abc)Capture group(foo)bar\1
(?:abc)Non-capturing group(?:foo|bar)+
(?<name>abc)Named capture group(?<year>\d{4})
(a|b)Alternation (OR)(cat|dog)

Lookaround

SyntaxDescriptionExample
(?=...)Positive lookahead\d+(?= dollars)
(?!...)Negative lookahead\d+(?! dollars)
(?<=...)Positive lookbehind(?<=\$)\d+
(?<!...)Negative lookbehind(?<!\$)\d+

Character Sets

SyntaxDescriptionExample
[abc]Any of a, b, or c[aeiou]
[^abc]Not a, b, or c[^\d]
[a-z]Range a to z[a-zA-Z]
[A-Z0-9]Uppercase letters or digits[A-Z0-9_]

Flags

SyntaxDescriptionExample
gGlobal — find all matches/pattern/g
iCase-insensitive/hello/i
mMultiline — ^ and $ match line boundaries/^line/m
sdotAll — . matches newline too/a.b/s
uUnicode mode/\u{1F600}/u
ySticky — match at lastIndex only/\d/y

Common Patterns

SyntaxDescriptionExample
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}Email addressuser@example.com
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&/=]*)URLhttps://example.com/path?q=1
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\bIPv4 address192.168.1.1
^#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$Hex color code#FF5733
^\d{4}-\d{2}-\d{2}$Date YYYY-MM-DD2024-01-15
^(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\d{3}\)|\d{3})\s*(?:[.-]\s*)?\d{3}\s*(?:[.-]\s*)?\d{4}$US Phone number+1 (555) 123-4567
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13})$Credit card (Visa/MC/Amex)4111111111111111