Skip to content

Instantly share code, notes, and snippets.

@lreverchuk
Last active June 13, 2026 10:58
Show Gist options
  • Select an option

  • Save lreverchuk/e199be5e922e95bd6d75203604d37521 to your computer and use it in GitHub Desktop.

Select an option

Save lreverchuk/e199be5e922e95bd6d75203604d37521 to your computer and use it in GitHub Desktop.
Regex Cheatsheet: Patterns, Anchors & Common Examples

Regex Cheatsheet: Patterns, Anchors & Common Examples

A regular-expression reference that works across most engines (PCRE, JavaScript, Python, Java, Go). Character classes, quantifiers, groups, lookarounds, plus copy-paste patterns for the things people actually match: email, URL, dates, IPs.

Character classes

Pattern Matches
. any character except newline
\d digit [0-9]
\D non-digit
\w word char [A-Za-z0-9_]
\W non-word char
\s whitespace (space, tab, newline)
\S non-whitespace
[abc] a, b, or c
[^abc] NOT a, b, or c
[a-z] range a to z
[a-zA-Z0-9] alphanumeric

Anchors & boundaries

Pattern Matches
^ start of string/line
$ end of string/line
\b word boundary
\B non-word boundary
\A \Z start / end of whole string (PCRE/Python)

Quantifiers

Pattern Means
* 0 or more
+ 1 or more
? 0 or 1 (optional)
{3} exactly 3
{2,5} between 2 and 5
{2,} 2 or more
*? +? lazy (as few as possible)

Groups & alternation

(abc)         capturing group
(?:abc)       non-capturing group
(?<name>abc)  named group
a|b           a OR b
\1            backreference to group 1

Lookarounds

Pattern Means
(?=foo) positive lookahead (followed by foo)
(?!foo) negative lookahead (NOT followed by foo)
(?<=foo) positive lookbehind (preceded by foo)
(?<!foo) negative lookbehind

Flags

Flag Effect
i case-insensitive
g global (all matches, JS)
m multiline (^/$ match each line)
s dotall (. matches newline)

Copy-paste patterns

# Email (practical, not RFC-perfect)
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

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

# IPv4 address
\b(?:\d{1,3}\.){3}\d{1,3}\b

# Date YYYY-MM-DD
\d{4}-\d{2}-\d{2}

# Time HH:MM (24h)
([01]\d|2[0-3]):[0-5]\d

# Hex color
#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b

# US phone
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}

# Strong password (8+, upper, lower, digit, symbol)
(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{8,}

# Leading/trailing whitespace
^\s+|\s+$

# Duplicate words
\b(\w+)\s+\1\b

Usage by language

// JavaScript
"a1b2".replace(/\d/g, "#");          // "a#b#"
const m = str.match(/(\d{4})-(\d{2})/);
# Python
import re
re.findall(r"\d+", "a1b22")          # ['1', '22']
re.sub(r"\s+", " ", text)            # collapse whitespace
m = re.search(r"(?P<year>\d{4})", s); m.group("year")
# grep (extended)
grep -E "[0-9]{3}-[0-9]{4}" file

Quick reference table

Want Pattern
One or more digits \d+
Optional char x?
Word boundary \b
Non-capturing group (?:...)
Lazy match *?
Case-insensitive flag i
Email [\w.%+-]+@[\w.-]+\.[A-Za-z]{2,}
IPv4 (?:\d{1,3}\.){3}\d{1,3}

Maintained by the team at EchoGlobal. Hiring developers? See our curated lists of Top JavaScript Engineers, Top Python Experts, and Top PHP Experts on GitHub, or hire pre-vetted developers in days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment