Overview

Regular expressions (regex) are patterns used to match character combinations in strings. They’re powerful tools for:

  • Text searching and validation
  • String manipulation and replacement
  • Data extraction and parsing
  • Input validation (emails, phone numbers, etc.)

Basic Syntax

See Regular Expression - Basic Syntax.

Advanced Features

See Regular Expression - Advanced Features.

Common Patterns

Email Validation

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

Breaks down to:

  • ^ - Start of string
  • [a-zA-Z0-9._%+-]+ - Username (letters, digits, special chars)
  • @ - Literal @
  • [a-zA-Z0-9.-]+ - Domain name
  • \. - Literal dot
  • [a-zA-Z]{2,} - TLD (2+ letters)
  • $ - End of string

Phone Number (US)

^(\+1[-.]?)?\(?([0-9]{3})\)?[-.]?([0-9]{3})[-.]?([0-9]{4})$

Matches:

  • 123-456-7890
  • (123) 456-7890
  • +1-123-456-7890
  • 1234567890

URL

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

Matches:

  • http://example.com
  • https://www.example.com/path?query=value

Date (YYYY-MM-DD)

^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$

Matches:

  • 2024-01-15
  • 2024-12-31

IP Address (IPv4)

^((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]?)$

Matches:

  • 192.168.1.1
  • 255.255.255.255

Hexadecimal Color

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

Matches:

  • #FF5733
  • #F57

Username (Alphanumeric + Underscore, 3-16 cHars)

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

Password (Minimum 8 Chars, 1 Uppercase, 1 Lowercase, 1 dIgit)

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

Flags/Modifiers

Common regex flags:

FlagMeaningExample
iCase insensitive/hello/i matches “Hello”, “HELLO”
gGlobal (find all matches)/a/g finds all ‘a’s
mMultiline (^ and $ match line boundaries)/^hello/m
sDot matches newlines/.*/s
xExtended (ignore whitespace, allow comments)
uUnicode/\u{1F600}/u

Language-Specific Syntax

See Regular Expression - Language-Specific Syntax.

Tips and Best Practices

See Regular Expression - Tips and Best Practices.

Debugging Regex

Common Mistakes

1. Forgetting to escape special characters:

Wrong: .pdf$
Right: \.pdf$

2. Greedy quantifiers matching too much:

Wrong: <div>.*</div> # Matches everything between first and last div
Right: <div>.*?</div> # Matches each div separately

3. Missing anchors:

Wrong: \d{3}-\d{4} # Matches substring
Right: ^\d{3}-\d{4}$ # Matches entire string

Testing Tools

Online Regex Testers

  • regex101.com - Best all-around tester
    • Real-time testing
    • Detailed explanation
    • Multiple flavors (PCRE, JavaScript, Python)
    • Save and share patterns
  • regexr.com - Visual and interactive
  • regextester.com - Simple and fast

Command Line

# grep (basic regex)
grep 'pattern' file.txt
 
# grep (extended regex)
grep -E 'pattern' file.txt
 
# sed (stream editor)
sed 's/pattern/replacement/g' file.txt
 
# awk
awk '/pattern/ {print}' file.txt

Common Use Cases

Data Extraction

# Extract emails from text
emails = re.findall(r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}', text)
 
# Extract URLs
urls = re.findall(r'https?://[^\s]+', text)
 
# Extract phone numbers
phones = re.findall(r'\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}', text)

Validation

def is_valid_email(email):
    pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
    return bool(re.match(pattern, email))
 
def is_valid_password(password):
    # Min 8 chars, 1 uppercase, 1 lowercase, 1 digit
    pattern = r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$'
    return bool(re.match(pattern, password))

Text Cleaning

# Remove extra whitespace
text = re.sub(r'\s+', ' ', text)
 
# Remove HTML tags
text = re.sub(r'<[^>]+>', '', html)
 
# Normalize phone numbers
phone = re.sub(r'[^0-9]', '', phone_str)

References