Skip to content

Regex Cheat Sheet

Regular Expressions (Regex) are patterns used to match, search, validate, and manipulate text.


Quantifiers (Repetitions)

Symbol Meaning Example
* 0 or more ab*
+ 1 or more ab+
? 0 or 1 ab?
{n} Exactly n times a{3}
{n,} At least n times a{2,}
{n,m} Between n and m a{2,5}

Anchors (Positions)

Anchor Description
^ Start of string
$ End of string
\b Word boundary
\B Not a word boundary

Character Classes

Shorthand

Class Meaning
\d Digit
\D Non-digit
\w Word character
\W Non-word character
\s Whitespace
\S Non-whitespace

POSIX

Class Meaning
[[:digit:]] Digits
[[:lower:]] Lowercase
[[:upper:]] Uppercase
[[:alpha:]] Letters
[[:alnum:]] Alphanumeric
[[:xdigit:]] Hex digits

Groups & References

Syntax Description
(abc) Capturing group
(?:abc) Non-capturing
\1 Backreference
(?P<name>...) Named group (Python)

Lookarounds

Type Syntax
Lookahead (?=...)
Negative lookahead (?!...)
Lookbehind (?<=...)
Negative lookbehind (?<!...)

Flags

Flag Meaning
i Case-insensitive
g Global
m Multiline
s Dot matches newline
x Verbose mode

Common Patterns

Purpose Regex
Email ^[\w.-]+@[\w.-]+\.[A-Za-z]{2,}$
URL https?://[^\s]+
IPv4 \b(?:\d{1,3}\.){3}\d{1,3}\b
Hex color #(?:[0-9a-fA-F]{3}){1,2}
Date (YYYY-MM-DD) \d{4}-\d{2}-\d{2}

Escaped Characters

Escape Meaning
\n Newline
\t Tab
\r Carriage return
\. Literal dot
\+ Literal plus

Tips

  • Always escape special characters when matching literals
  • Prefer non-capturing groups when captures are unnecessary
  • Test regex with multiple inputs
  • Avoid catastrophic backtracking

Engines

Supported by most engines: - PCRE - Python - JavaScript - POSIX (partial)