NetLabToolsNetLabTools

Regex Cheatsheet – essential patterns with examples

All key regex patterns at a glance: character classes, quantifiers, anchors, lookarounds and ready-to-use patterns for email, URL and more.

8 min read

Regular expressions are one of the most powerful tools in text processing — and one of the most cryptic. This cheatsheet covers the essential building blocks with examples you can copy straight into our regex tester.

Character classes

Character classes match exactly one character from a defined set. They are the foundation of almost every regex.

SymbolMeaningExample
\dDigit 0–9\d{3} matches 123
\DNon-digit\D+ matches abc
\wWord character (a-z, A-Z, 0-9, _)\w+ matches hello_42
\WNon-word characterspaces, commas, etc.
\sWhitespace (space, tab, newline)\s+
\SNon-whitespace
[a-z]Range of lowercase letters[a-zA-Z0-9]
[^abc]Negation: not a, b, or c
.Any character (except newline)
\.Literal dot

Quantifiers

Quantifiers control how often the preceding element may appear.

  • * – 0 or more
  • + – 1 or more
  • ? – 0 or 1 (optional)
  • {n} – exactly n times
  • {n,m} – between n and m times
  • {n,} – at least n times

Quantifiers are greedy by default and match as much as possible. Append ? to make them lazy: a.+?b matches the shortest a...b instead of the longest.

Anchors and word boundaries

  • ^ – start of line/string
  • $ – end of line/string
  • \b – word boundary (between \w and \W)
  • \B – not a word boundary

With the multiline flag m, ^ and $ match every line, not only the whole string.

Groups, alternation and lookarounds

Parentheses ( ... ) form a capturing group. Use(?: ... ) for a non-capturing group — faster and cleaner when you don't need the match back. Alternation uses |: cat|dog.

Lookarounds

  • (?=...) – positive lookahead
  • (?!...) – negative lookahead
  • (?<=...) – positive lookbehind
  • (?<!...) – negative lookbehind

Common patterns to copy

# Email (simplified — RFC-compliant is much longer)
^[\w.+-]+@[\w-]+\.[a-zA-Z]{2,}$

# URL (http/https)
^https?://[\w.-]+(?::\d+)?(?:/[^\s]*)?$

# IPv4
^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$

# ISO date YYYY-MM-DD
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$

# German postal code
^\d{5}$

# US ZIP (5 or 5+4)
^\d{5}(-\d{4})?$

Engine differences

Heads up: not every regex engine is equal. JavaScript supports lookbehinds since ES2018, older browsers don't. Go (RE2) skips backreferences and lookarounds in favor of linear runtime guarantees. Python addsre.VERBOSE for readable patterns. When in doubt: test, don't guess.