G GenMB Tools Regex Cheatsheet & Reference

Regex Cheatsheet

A clean searchable reference of regex syntax with copyable examples.

Anchors

^Start of string (or line in m mode)
$End of string (or line in m mode)
\bWord boundary
\BNon-word boundary

Character classes

.Any character except newline
\dDigit (0-9)
\DNon-digit
\wWord character (a-z, A-Z, 0-9, _)
\WNon-word character
\sWhitespace
\SNon-whitespace
[abc]Any of a, b, or c
[^abc]Not a, b, or c
[a-z]Range a through z

Quantifiers

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

Groups & references

(...)Capturing group
(?:...)Non-capturing group
(?<name>...)Named capturing group
\1, \2Backreference to group
\k<name>Backreference to named group

Lookarounds

(?=...)Positive lookahead
(?!...)Negative lookahead
(?<=...)Positive lookbehind
(?<!...)Negative lookbehind

Flags

gGlobal - find all matches
iCase-insensitive
mMultiline - ^ and $ match line boundaries
sDotall - . matches newline
uUnicode mode
ySticky - match at lastIndex only

Common patterns

^[\w.+-]+@[\w-]+\.[\w.-]+$Email (pragmatic)
^https?://\S+$URL (http or https)
^\d{3}-?\d{3}-?\d{4}$US phone
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ISO date YYYY-MM-DD
^[a-z0-9]+(?:-[a-z0-9]+)*$URL slug
^#?([\dA-Fa-f]{3}|[\dA-Fa-f]{6})$Hex color
Built with GenMB
Built with GenMB