๐Ÿงฉ Regular Expressions (Regex) Cheatsheet

A concise, one-page reference for the most common regex tokens and patterns.


๐Ÿ”ค Literals & Escaping

PatternMeaning
abcMatches literal abc
\.Escaped dot (matches .)
\\Escaped backslash
\nNew line
\tTab

๐ŸŽฏ Character Classes

PatternMeaning
.Any character except newline
\dDigit [0-9]
\DNon-digit
\wWord character [A-Za-z0-9_]
\WNon-word character
\sWhitespace
\SNon-whitespace
[abc]Any of a, b, or c
[^abc]Not a, b, or c
[a-z]Range from a to z

๐Ÿ“ Anchors (Position)

PatternMeaning
^Start of string
$End of string
\bWord boundary
\BNon-word boundary
\AStart of input (some engines)
\ZEnd of input (some engines)

๐Ÿ” Quantifiers

PatternMeaning
*0 or more
+1 or more
?0 or 1
{n}Exactly n times
{n,}At least n times
{n,m}Between n and m times
*? +? ??Lazy (non-greedy) versions

๐Ÿ”— Grouping & Alternation

PatternMeaning
(abc)Capturing group
(?:abc)Non-capturing group
(?<name>abc)Named capturing group
`ab`
\1Backreference to group 1
\k<name>Backreference to named group

๐Ÿ” Lookarounds (Zero-width Assertions)

PatternMeaning
(?=abc)Positive lookahead
(?!abc)Negative lookahead
(?<=abc)Positive lookbehind
(?<!abc)Negative lookbehind

๐Ÿง  Flags / Modifiers

FlagMeaning
iCase-insensitive
gGlobal search
mMultiline (^ and $)
sDot matches newline
uUnicode
xIgnore whitespace/comments

๐Ÿงช Common Patterns

Use CasePattern
Email^[\w.-]+@[\w.-]+\.\w+$
URLhttps?:\/\/[\w.-]+
IPv4\b(?:\d{1,3}\.){3}\d{1,3}\b
Date (YYYY-MM-DD)\d{4}-\d{2}-\d{2}
HTML tag<[^>]+>

๐Ÿ› ๏ธ Tips

  • Prefer specific patterns over .*
  • Use lazy quantifiers to avoid overmatching
  • Test regex with tools like regex101.com
  • Remember: regex engines differ (PCRE, JS, Python)

๐Ÿ“Œ **Regex is powerfulโ€”but readability matters. Comment complex patterns

ยฉ 2025 Mauro Leonelli. All Rights Reserved.