ENGIMY.IO - CHEATSHEET
REGEX × QUICK REFERENCE
REFERENCE v1.0

Regex Patterns Quick Reference

Everything you need day‑to‑day – from basic matching to advanced replacements.

Regex Basics

What is Regex?
  • Sequence of characters for pattern matching
  • Used in: grep, sed, awk, Python, JavaScript, Java, etc.
  • Delimiters: /pattern/flags (JavaScript), pattern (Python)
  • Flags: g (global), i (case‑insensitive), m (multiline), s (dotall), u (unicode)
Literal Characters
  • Letters and digits match themselves
  • hello – matches the exact string "hello"
  • 123 – matches the digits "123"
  • Special characters must be escaped: \. \* \+ \? \( \) \[ \] \{ \} \| \^ \$ \\
  • \. – matches a literal dot

Anchors

Pattern Description Example
^ Start of string/line ^Hello – matches "Hello" at start
$ End of string/line world$ – matches "world" at end
\b Word boundary \bword\b – matches whole word "word"
\B Not word boundary \Bword – matches "word" not at boundary
\A Start of string \AHello – matches "Hello" at start
\Z End of string world\Z – matches "world" at end
\z End of string (strict) world\z – no trailing newline allowed

Character Classes

Pattern Description Example
. Any character (except newline) c.t – matches "cat", "cut", etc.
\d Digit (0-9) \d+ – matches one or more digits
\D Not a digit \D+ – matches non‑digits
\w Word character (alnum + _) \w+ – matches word
\W Not a word character \W+ – matches non‑word characters
\s Whitespace \s+ – matches whitespace
\S Not whitespace \S+ – matches non‑whitespace
[abc] Any of a, b, or c [aeiou] – matches vowels
[^abc] Not a, b, or c [^aeiou] – matches consonants
[a-z] Lowercase letters [a-zA-Z] – matches any letter
[0-9] Digits [0-9] – same as \d
\p{L} Unicode letter (Python/Java) \p{L}+ – matches letters in any script
\p{N} Unicode number \p{N} – matches numbers

Quantifiers

Pattern Description Example
* Zero or more (greedy) a* – matches "", "a", "aa", ...
+ One or more (greedy) a+ – matches "a", "aa", ...
? Zero or one (greedy) a? – matches "" or "a"
{n} Exactly n times a{3} – matches "aaa"
{n,} At least n times a{2,} – matches "aa", "aaa", ...
{n,m} Between n and m times a{2,4} – matches "aa", "aaa", "aaaa"
*? Zero or more (lazy) a*? – shortest match
+? One or more (lazy) a+? – shortest "a"
?? Zero or one (lazy) a?? – matches ""
{n,m}? Lazy quantifier a{2,4}? – matches "aa"
*+ Possessive (no backtracking) a*+ – behaves like greedy but fails faster
++ Possessive (no backtracking) a++ – same as above

Groups & Capturing

Pattern Description Example
() Capturing group (\\d+) – captures digits
(?:) Non‑capturing group (?:\\d+) – groups without capturing
(?P<name>) Named group (Python) (?P<year>\\d{4})
\g<name> Backreference to named group \g<year>
$1, \1 Backreference to group 1 Replacement: re.sub(r'(\\d+)', r'$1', text)
| Alternation (OR) cat|dog – matches "cat" or "dog"

Lookarounds

Pattern Description Example
(?=...) Positive lookahead \\d+(?= dollars) – digits before "dollars"
(?!...) Negative lookahead \\d+(?! dollars) – digits not before "dollars"
(?<=...) Positive lookbehind (?<=\\$)\\d+ – digits after "$"
(?<!...) Negative lookbehind (?<!\\$)\\d+ – digits not after "$"

Common Patterns

Email Address

^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$

Phone Number (US)

^\\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}$

URL

^(https?|ftp)://[^\\s/$.?#].[^\\s]*$

IP Address (IPv4)

\\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b

Date (YYYY-MM-DD)

^\\d{4}-\\d{2}-\\d{2}$

Time (HH:MM:SS)

^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$

Credit Card (Visa/Mastercard)

^\\d{4}[-\\s]?\\d{4}[-\\s]?\\d{4}[-\\s]?\\d{4}$

Username (alphanumeric, 3-16 chars)

^[a-zA-Z0-9_]{3,16}$

Password (strong)

^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[@$!%*?&]).{8,}$

Hex Color

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$

HTML Tag

<\/?[a-z][a-z0-9]*[^<>]*>

UUID (v4)

^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$

Regex Flags

Flag Description Usage
g Global (find all matches) /pattern/g (JavaScript)
i Case‑insensitive /pattern/i
m Multiline (^ and $ match line boundaries) /pattern/m
s Dotall (. matches newline) /pattern/s
u Unicode /pattern/u (JavaScript)
re.DOTALL Dotall (Python) re.compile(pattern, re.DOTALL)
re.MULTILINE Multiline (Python) re.compile(pattern, re.MULTILINE)

Regex in Different Languages

Python

import re

# Match
pattern = r'\d+'
text = 'There are 42 apples'
match = re.search(pattern, text)
if match:
    print(match.group())  # '42'

# Find all
results = re.findall(r'\d+', text)  # ['42']

# Replace
new_text = re.sub(r'\d+', 'X', text)  # 'There are X apples'

# Compile
compiled = re.compile(r'\d+', re.IGNORECASE)
compiled.match(text)

# Named groups
match = re.search(r'(?P<year>\d{4})', '2024')
year = match.group('year')  # '2024'

JavaScript

// Match
const pattern = /\d+/;
const text = 'There are 42 apples';
const match = text.match(pattern);
console.log(match[0]);  // '42'

// Find all
const matches = text.match(/\d+/g);  // ['42']

// Replace
const newText = text.replace(/\d+/g, 'X');  // 'There are X apples'

// Test
const isMatch = /\d+/.test(text);  // true

// Named groups (ES2018+)
const match = /(?<year>\d{4})/.exec('2024');
console.log(match.groups.year);  // '2024'

Java

import java.util.regex.*;

Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("There are 42 apples");
if (matcher.find()) {
    System.out.println(matcher.group());  // '42'
}

// Replace
String newText = "There are 42 apples".replaceAll("\\d+", "X");

// Named groups (Java 8+)
Pattern namedPattern = Pattern.compile("(?<year>\\d{4})");
Matcher namedMatcher = namedPattern.matcher("2024");
if (namedMatcher.find()) {
    System.out.println(namedMatcher.group("year"));  // '2024'
}

grep

# Basic grep
grep pattern file.txt

# Extended regex
grep -E 'pattern' file.txt

# Case‑insensitive
grep -i pattern file.txt

# Recursive
grep -r pattern .

# Count matches
grep -c pattern file.txt

# Invert match
grep -v pattern file.txt

# Show line numbers
grep -n pattern file.txt

sed

# Replace first occurrence
sed 's/old/new/' file.txt

# Replace all
sed 's/old/new/g' file.txt

# In‑place replace
sed -i 's/old/new/g' file.txt

# Replace with capture groups
sed -r 's/(\\d+)/Number: \\1/g' file.txt

# Delete matching lines
sed '/pattern/d' file.txt

Escaping Special Characters

Character Escaped Description
. \. Literal dot
* \* Literal asterisk
+ \+ Literal plus
? \? Literal question mark
{ \{ Literal opening brace
} \} Literal closing brace
( \( Literal opening parenthesis
) \) Literal closing parenthesis
[ \[ Literal opening bracket
] \] Literal closing bracket
| \| Literal pipe
^ \^ Literal caret
$ \$ Literal dollar
\ \\ Literal backslash

Common Pitfalls

  • Greedy vs Lazy – greedy (.*) matches as much as possible, lazy (.*?) matches as little as possible
  • Escaping – in strings, backslashes often need to be escaped (\\d in Python, \d in JavaScript)
  • Unicode – use \p{L} for letters (Python/Java) or \p{Letter} for any language
  • Performance – avoid catastrophic backtracking (e.g., (a+)*b)
  • Group numbering – non‑capturing groups ((?:...)) don't increment group numbers
  • Lookbehind – not all languages support variable‑length lookbehind ((?<=ab|cd))
  • Use raw strings – in Python, use r"pattern" to avoid escaping backslashes
📌 Quick Reference
Anchors: ^ (start), $ (end), \b (word boundary)
Classes: \d (digit), \w (word), \s (whitespace), . (any char)
Quantifiers: * (0+), + (1+), ? (0/1), {n,m}
Groups: () (capturing), (?:) (non‑capturing)
Lookarounds: (?=) (lookahead), (?<=) (lookbehind)
Flags: g (global), i (case‑insensitive), m (multiline)
Tools: Python (re), JavaScript (/pattern/), Java (Pattern), grep, sed, awk
← Back to All Cheatsheets