0% found this document useful (0 votes)
8 views3 pages

Regex Patterns Reference

This document serves as a quick reference guide for regular expressions, detailing basic syntax, anchors, quantifiers, character classes, grouping, and common patterns. It includes examples for validating emails, phone numbers, IP addresses, and more, along with performance tips and testing tools. The guide also covers replacement patterns and lookahead/lookbehind assertions.

Uploaded by

PaleWalker47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Regex Patterns Reference

This document serves as a quick reference guide for regular expressions, detailing basic syntax, anchors, quantifiers, character classes, grouping, and common patterns. It includes examples for validating emails, phone numbers, IP addresses, and more, along with performance tips and testing tools. The guide also covers replacement patterns and lookahead/lookbehind assertions.

Uploaded by

PaleWalker47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

REGULAR EXPRESSIONS QUICK REFERENCE

==================================

=== BASIC SYNTAX ===


. # Match any single character (except newline)
\d # Match any digit (0-9)
\D # Match any non-digit
\w # Match any word character (a-z, A-Z, 0-9, _)
\W # Match any non-word character
\s # Match any whitespace character
\S # Match any non-whitespace character
\n # Match newline
\t # Match tab
\\ # Match literal backslash

=== ANCHORS ===


^ # Start of string/line
$ # End of string/line
\b # Word boundary
\B # Non-word boundary
\A # Start of string only
\Z # End of string only

=== QUANTIFIERS ===


* # 0 or more (greedy)
+ # 1 or more (greedy)
? # 0 or 1 (optional)
{n} # Exactly n times
{n,} # n or more times
{n,m} # Between n and m times
*? # 0 or more (non-greedy)
+? # 1 or more (non-greedy)
?? # 0 or 1 (non-greedy)

=== CHARACTER CLASSES ===


[abc] # Match any of a, b, c
[a-z] # Match any lowercase letter
[A-Z] # Match any uppercase letter
[0-9] # Match any digit
[a-zA-Z0-9] # Match any alphanumeric character
[^abc] # Match anything except a, b, c
[^0-9] # Match any non-digit

=== GROUPING & ALTERNATION ===


(abc) # Capture group
(?:abc) # Non-capturing group
(?P<name>abc) # Named capture group (Python)
a|b # Match a OR b
(a|b)c # Match ac OR bc

=== ESCAPE SEQUENCES ===


\. # Literal dot
\* # Literal asterisk
\+ # Literal plus
\? # Literal question mark
\[ # Literal opening bracket
\] # Literal closing bracket
\( # Literal opening parenthesis
\) # Literal closing parenthesis
\{ # Literal opening brace
\} # Literal closing brace

=== COMMON PATTERNS ===

# Email validation (basic)


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

# Phone number (US format)


^\(\d{3}\)\s\d{3}-\d{4}$ # (123) 456-7890
^\d{3}-\d{3}-\d{4}$ # 123-456-7890

# IP Address
^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$

# URL validation (basic)


^https?:\/\/[^\s/$.?#].[^\s]*$

# Password (8+ chars, number, uppercase, lowercase)


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

# Credit card number (remove spaces/dashes)


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

# Date formats
^\d{4}-\d{2}-\d{2}$ # YYYY-MM-DD
^\d{1,2}\/\d{1,2}\/\d{4}$ # MM/DD/YYYY
^\d{1,2}-\d{1,2}-\d{4}$ # MM-DD-YYYY

# Time format (24-hour)


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

# HTML tags
<[^>]+> # Any HTML tag
<\/?\w+(?:\s+\w+(?:=(?:"[^"]*"|'[^']*'|[^>\s]+))?)*\s*\/?>

# Extract text between quotes


"([^"]*)" # Double quotes
'([^']*)' # Single quotes

# File extensions
\.(jpg|jpeg|png|gif|bmp)$ # Image files
\.(pdf|doc|docx|txt)$ # Document files

# Whitespace cleanup
^\s+|\s+$ # Leading/trailing whitespace
\s{2,} # Multiple spaces

# Numbers
^-?\d+$ # Integer (positive or negative)
^-?\d+\.?\d*$ # Decimal number
^\d{1,3}(,\d{3})*$ # Number with commas (1,000)

=== LOOKAHEAD/LOOKBEHIND ===


(?=...) # Positive lookahead
(?!...) # Negative lookahead
(?<=...) # Positive lookbehind
(?<!...) # Negative lookbehind
# Password with lookahead (must contain number and letter)
^(?=.*\d)(?=.*[a-zA-Z]).{8,}$

=== FLAGS/MODIFIERS ===


i # Case insensitive
g # Global match (find all)
m # Multiline mode
s # Dot matches newline
x # Extended (ignore whitespace)
u # Unicode

=== REPLACEMENT PATTERNS ===


$1, $2, $3 # Reference capture groups (most languages)
\1, \2, \3 # Reference capture groups (some languages)
$& # Entire match
$` # Text before match
$' # Text after match

=== COMMON USE CASES ===

# Remove HTML tags


s/<[^>]*>//g

# Extract domain from email


@([a-zA-Z0-9.-]+\.[a-zA-Z]{2,})

# Validate hex color


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

# Clean phone number (keep digits only)


[^\d]

# Extract file name from path


[^/\\]*$

# Match whole words only


\bword\b

# Match lines starting with specific text


^text.*

# Match empty lines


^\s*$

=== TESTING TOOLS ===


regex101.com # Online regex tester
regexr.com # Interactive regex learning
regexpal.com # Simple regex testing

=== PERFORMANCE TIPS ===


- Use non-capturing groups (?:) when you don't need the match
- Be specific with character classes instead of using .
- Anchor patterns when possible (^ and $)
- Use atomic groups for better performance
- Avoid excessive backtracking with greedy quantifiers

You might also like