regexregular expressionscheatsheetpatterns

Regular Expressions: Cheatsheet With Examples

Regex cheatsheet: metacharacters, classes, quantifiers, anchors, groups, lookarounds, ready patterns for email, phone and URL, and common mistakes.

June 22, 2026·5 min read

A regular expression (regex) is a pattern that describes text, to search it, validate it or replace it. This cheatsheet gathers the essential syntax, with examples and patterns you can copy. To test each pattern on your own text, use the regex tester.

Basic metacharacters

Symbol Means Example
. Any character (except newline) a.c → "abc", "axc"
\ Escapes a special character \. → a literal dot
| OR (alternation) cat|dog
^ Start of string (or of line with the m flag) ^Hello
$ End of string (or of line with the m flag) end$

Character classes

Pattern Matches
[abc] An a, b or c
[^abc] Any character that is NOT a, b or c
[a-z] Any lowercase
[A-Z] Any uppercase
[0-9] Any digit
[a-zA-Z0-9] Letters and numbers

Predefined classes (shortcuts)

Shortcut Equals Means
\d [0-9] A digit
\D [^0-9] NOT a digit
\w [a-zA-Z0-9_] Word character
\W [^a-zA-Z0-9_] NOT a word character
\s space, tab, newline Whitespace
\S — NOT whitespace

Quantifiers (how many times)

Symbol Means Example
* 0 or more a*
+ 1 or more a+
? 0 or 1 (optional) colou?r → "color" or "colour"
{n} Exactly n \d{4} → 4 digits
{n,} n or more \d{2,}
{n,m} Between n and m \d{2,4}

By default quantifiers are greedy (take the maximum). Add ? to make them lazy (the minimum): .*?.

Anchors and boundaries

Pattern Means
^ Start of string/line
$ End of string/line
\b Word boundary
\B NOT a word boundary

\bcat\b finds "cat" as a whole word, not inside "category".

Groups and capture

Pattern Means
(abc) Capture group
(?:abc) NON-capturing group
(?<name>abc) Named group
\1 Reference to group 1

Groups are used to extract parts ((\d{4})-(\d{2}) captures year and month separately) and to apply quantifiers to several characters: (ab)+.

Lookarounds (look around without consuming)

Pattern Means
(?=...) Positive lookahead (followed by)
(?!...) Negative lookahead (NOT followed by)
(?<=...) Positive lookbehind (preceded by)
(?<!...) Negative lookbehind

Example: \d+(?= €) captures the number only if followed by " €", without including the symbol.

Flags (modifiers)

Flag Effect
g Global (all matches, not just the first)
i Case-insensitive
m Multiline (^ and $ per line)
s . also matches newlines
u Unicode: . matches a full code point, such as an emoji

Ready-to-use patterns

Email (simple):        ^[\w.+-]+@[\w-]+\.[\w.-]+$
Phone (Spain):         ^(\+34[\s-]?)?[6789](?:[\s-]?\d){8}$
URL:                   ^https?:\/\/[^\s]+$
Postal code (Spain):   ^\d{5}$
Numbers only:          ^\d+$
Date YYYY-MM-DD:       ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$
Hex color:             ^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$
Repeated spaces:       \s{2,}

Note: validating emails with a regex 100% perfectly is nearly impossible. The HTML specification by WHATWG uses a much longer expression. For most forms the simple pattern is enough, although it accepts addresses the specification rejects, such as ana@-x.es. The date pattern checks month and day by range, but not the calendar: February 30 passes.

Tip: always test your regex

A regex can look correct and fail on edge cases. The best way to refine it is to test it with real text and see what it captures. You can do it instantly with the regex tester on this site, which highlights matches as you type the pattern.

Common mistakes

  • Forgetting to escape special characters: to find a literal dot use \., not ..
  • Unexpected greed: <.*> on <a><b> captures everything at once; use <.*?> to stop at the first >.
  • Not using the g flag when you want all matches.
  • Anchoring wrong: without ^ and $, a pattern can match a part when you wanted the whole string.
  • Catastrophic backtracking: (a+)+$ on a run of letters followed by a character that doesn't fit makes the engine try combinations exponentially. The tester stops the search after 2 seconds and warns you. In your code, avoid nested quantifiers over the same text. More detail in advanced regex: backtracking.

Conclusion

Regex is easiest to understand as combinable pieces: classes, quantifiers, anchors and groups. With this cheatsheet you have the vocabulary and the patterns; before you trust a new one, test it with real text.

Try it without code

Regex Tester

Real-time regex with match highlights.

Open Regex Tester

Built by

Miguel Ángel Colorado Marin (MACM)

Full-Stack Developer · Guadalajara, España

I develop web apps, digital tools and full projects — from design to deployment.

Contact me