Regex (Regular Expressions) Cheat Sheet

Regular Expressions (Regex) can seem complicated and painful to work with at first, but once you get the hang of them, they make a lot of sense. They're an essential tool for developers when working with text, helping to search, match, and manipulate patterns efficiently.

This cheat sheet explains each regex notation in detail with practical examples.


1. Basic Syntax

Pattern Meaning
. Matches any single character except a newline (\n).
^ Matches the start of a string.
$ Matches the end of a string.
\ Escape character—used before special characters to match them literally (e.g., \. matches a period .).

Examples:

  • c.t → Matches cat, cut, cot, etc.
  • ^Hello → Matches Hello only if it’s at the beginning of a line.
  • world$ → Matches world only if it’s at the end of a line.

2. Character Classes

Pattern Meaning
[abc] Matches any of a, b, or c.
[^abc] Matches any character except a, b, or c.
[a-z] Matches any lowercase letter from a to z.
[A-Z] Matches any uppercase letter from A to Z.
[0-9] Matches any digit from 0 to 9.
\d Matches any digit ([0-9]).
\D Matches any non-digit ([^0-9]).
\w Matches any word character ([a-zA-Z0-9_]).
\W Matches any non-word character ([^a-zA-Z0-9_]).
\s Matches any whitespace character ([ \t\n\r\f\v]).
\S Matches any non-whitespace character ([^ \t\n\r\f\v]).

Examples:

  • [aeiou] → Matches any vowel.
  • [^0-9] → Matches anything that is not a digit.
  • \d+ → Matches 123, 56, 9999, etc.

3. Quantifiers

Pattern Meaning
* Matches 0 or more occurrences of the preceding pattern (greedy).
+ Matches 1 or more occurrences of the preceding pattern.
? Matches 0 or 1 occurrence of the preceding pattern (optional).
{n} Matches exactly n times.
{n,} Matches at least n times.
{n,m} Matches between n and m times.

Examples:

  • a* → Matches "", "a", "aa", "aaa", etc.
  • a+ → Matches "a", "aa", "aaa", etc. (at least one a)
  • a{3} → Matches exactly "aaa"
  • a{2,4} → Matches "aa", "aaa", or "aaaa"

4. Grouping and Capturing

Pattern Meaning
(abc) Capturing group—treats abc as a single unit and remembers it.
(?:abc) Non-capturing group—groups abc without storing it.
(?P<name>abc) Named capturing group—stores abc with a specific name.
\n Refers to the nth capturing group (e.g., \1 refers to the first group).

Examples:

  • (ab)+ → Matches "ab", "abab", "ababab", etc.
  • "(\d{3})-(\d{2})" applied to "123-45" → Captures "123" and "45" separately.

5. Assertions (Lookaheads & Lookbehinds)

Pattern Meaning
(?=...) Positive lookahead – Ensures that the pattern exists ahead, but does not include it in the match.
(?!...) Negative lookahead – Ensures that the pattern does not exist ahead.
(?<=...) Positive lookbehind – Ensures that the pattern exists before the match.
(?<!...) Negative lookbehind – Ensures that the pattern does not exist before the match.

Examples:

  • \d(?=px) → Matches a digit only if followed by "px".
  • (?<=\$)\d+ → Matches a number only if preceded by "$" (e.g., $100 → matches 100).

6. Useful Practical Examples

Pattern Matches
\d{4}-\d{2}-\d{2} Matches a date in YYYY-MM-DD format.
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b Matches a valid email address.
https?://[^\s]+ Matches a URL.
\(\d{3}\) \d{3}-\d{4} Matches a phone number in (123) 456-7890 format.

Good Luck Regular Expressioning I guess.