Practical Examples

Learn regular expressions by seeing patterns, test cases, and match results side-by-side

Overview

Real-world regex patterns for common tasks

01 Email

A practical email validation pattern (simplified version).

Pattern breakdown:

  • [A-Za-z0-9._%+-]+ - local part (before @)
  • @ - literal @ symbol
  • [A-Za-z0-9.-]+ - domain name
  • \. - literal period
  • [A-Z|a-z]{2,} - TLD (2+ letters)

Note: This is a simplified pattern. Production email validation should use more comprehensive patterns or specialized libraries.

Source: EmailRegex.com - Email Validation Patterns

Pattern: \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
Test Input
user@example.com
test.email+tag@domain.co.uk
invalid@
@invalid.com
valid_email123@test-domain.org
Match Results
Line 1: "user@example.com"
Match 1: "user@example.com"
Line 2: "test.email+tag@domain.co.uk"
Match 1: "test.email+tag@domain.co.uk"
Line 3: "invalid@" - No match
Line 4: "@invalid.com" - No match
Line 5: "valid_email123@test-domain.org"
Match 1: "valid_email123@test-domain.org"
All matches found: 3
Matches: ['user@example.com', 'test.email+tag@domain.co.uk', 'valid_email123@test-domain.org']

02 Phone

Match US phone numbers in various formats.

Pattern breakdown:

  • (?:\d{3}[-.]?)? - optional area code with optional separator
  • \d{3}[-.]? - 3 digits with optional separator
  • \d{4} - final 4 digits

This handles formats like 555-123-4567, 555.123.4567, 5551234567, and 123-4567.

Source: Regular-Expressions.info - Examples

Pattern: (?:(?:\(\d{3}\)|(?:\b\d{3}))[-.\s]?)?\b\d{3}[-.]?\d{4}\b
Test Input
555-123-4567
555.123.4567
5551234567
123-4567
(555) 123-4567
Please call (555) 123-4567 for assistance.
You can reach support at 800-555-0199 or optionally 800.555.0100.
My local number is 867-5309, call me maybe.
Our office line: 555 123 4567 is open 9am-5pm.
Ticket #123456789999 is not a phone number.
(425)555-0123 is valid even without a space after parens.
Match Results
Line 1: "555-123-4567"
Match 1: "555-123-4567"
Line 2: "555.123.4567"
Match 1: "555.123.4567"
Line 3: "5551234567" - No match
Line 4: "123-4567"
Match 1: "123-4567"
Line 5: "(555) 123-4567"
Match 1: "(555) 123-4567"
Line 6: "Please call (555) 123-4567 for assistance."
Match 1: "(555) 123-4567"
Line 7: "You can reach support at 800-555-0199 or optionally 800.555.0100."
Match 1: "800-555-0199"
Match 2: "800.555.0100"
Line 8: "My local number is 867-5309, call me maybe."
Match 1: "867-5309"
Line 9: "Our office line: 555 123 4567 is open 9am-5pm." - No match
Line 10: "Ticket #123456789999 is not a phone number." - No match
Line 11: "(425)555-0123 is valid even without a space after parens."
Match 1: "(425)555-0123"
All matches found: 9
Matches: ['555-123-4567', '555.123.4567', '123-4567', '(555) 123-4567', '(555) 123-4567', '800-555-0199', '800.555.0100', '867-5309', '(425)555-0123']

03 Url

Extract HTTP and HTTPS URLs from text.

Pattern breakdown:

  • https?:// - HTTP or HTTPS protocol
  • (?:www\.)? - optional www prefix
  • [-a-zA-Z0-9@:%._\+~#=]{1,256} - domain characters
  • \.[a-zA-Z0-9()]{1,6} - TLD
  • (?:[-a-zA-Z0-9()@:%_\+.~#?&/=]*) - optional path and query

Source: URLRegex.com - URL Matching Patterns

Pattern: https?://(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&/=]*)
Test Input
Visit https://example.com for more info.
Check out http://www.test-site.org/path/page.html
Link: https://subdomain.example.co.uk/path?query=value
Not a URL: example.com
Match Results
Line 1: "Visit https://example.com for more info."
Match 1: "https://example.com"
Line 2: "Check out http://www.test-site.org/path/page.html"
Match 1: "http://www.test-site.org/path/page.html"
Line 3: "Link: https://subdomain.example.co.uk/path?query=value"
Match 1: "https://subdomain.example.co.uk/path?query=value"
Line 4: "Not a URL: example.com" - No match
All matches found: 3
Matches: ['https://example.com', 'http://www.test-site.org/path/page.html', 'https://subdomain.example.co.uk/path?query=value']

04 Date

Match ISO 8601 date format (YYYY-MM-DD) with validation.

Pattern breakdown:

  • (?P<year>\d{4}) - 4-digit year
  • (?P<month>0[1-9]|1[0-2]) - month 01-12
  • (?P<day>0[1-9]|[12]\d|3[01]) - day 01-31

This validates month and day ranges (though doesn't check for valid days in each month).

Source: Regular-Expressions.info - Dates and Times

Pattern: \b(?P<year>\d{4})-(?P<month>0[1-9]|1[0-2])-(?P<day>0[1-9]|[12]\d|3[01])\b
Test Input
Meeting on 2024-01-15
Deadline: 2024-12-31
Invalid: 2024-13-45
Also check 2023-06-30
Match Results
Line 1: "Meeting on 2024-01-15"
Match 1: "2024-01-15"
Groups: 1: "2024", 2: "01", 3: "15"
Named: year: "2024", month: "01", day: "15"
Line 2: "Deadline: 2024-12-31"
Match 1: "2024-12-31"
Groups: 1: "2024", 2: "12", 3: "31"
Named: year: "2024", month: "12", day: "31"
Line 3: "Invalid: 2024-13-45" - No match
Line 4: "Also check 2023-06-30"
Match 1: "2023-06-30"
Groups: 1: "2023", 2: "06", 3: "30"
Named: year: "2023", month: "06", day: "30"
All matches found: 3
Matches: [('2024', '01', '15'), ('2024', '12', '31'), ('2023', '06', '30')]

05 Password

Validate passwords with multiple requirements using lookaheads.

Requirements:

  • (?=.*[a-z]) - at least one lowercase letter
  • (?=.*[A-Z]) - at least one uppercase letter
  • (?=.*\d) - at least one digit
  • (?=.*[@$!%*?&]) - at least one special character
  • [A-Za-z\d@$!%*?&]{8,} - 8+ characters total

This ensures strong password requirements are met.

Source: Regular-Expressions.info - Password Validation

Pattern: ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Test Input
Password123!
weakpass
NoSpecial123
NoDigits!
Short1!
ValidPass123!
Match Results
Line 1: "Password123!"
Match 1: "Password123!"
Line 2: "weakpass" - No match
Line 3: "NoSpecial123" - No match
Line 4: "NoDigits!" - No match
Line 5: "Short1!" - No match
Line 6: "ValidPass123!"
Match 1: "ValidPass123!"
All matches found: 2
Matches: ['Password123!', 'ValidPass123!']

06 Html Tags

Extract HTML tags and their content (simplified pattern).

Pattern breakdown:

  • <([a-z]+) - opening tag name (captured)
  • ([^>]*) - attributes
  • >(.*?)</\1> - content and matching closing tag
  • \1 - backreference to tag name

Note: This is simplified. Real HTML parsing should use proper HTML parsers, not regex.

Source: Stack Overflow - Why not parse HTML with regex

Pattern: <([a-z]+)([^>]*)>(.*?)</\1>
Test Input
<p>This is a paragraph.</p>
<div class="container">Content here</div>
<span>Text</span>
<img src="test.jpg" />
Match Results
Line 1: "<p>This is a paragraph.</p>"
Match 1: "<p>This is a paragraph.</p>"
Groups: 1: "p", 2: "", 3: "This is a paragraph."
Line 2: "<div class="container">Content here</div>"
Match 1: "<div class="container">Content here</div>"
Groups: 1: "div", 2: " class="container"", 3: "Content here"
Line 3: "<span>Text</span>"
Match 1: "<span>Text</span>"
Groups: 1: "span", 2: "", 3: "Text"
Line 4: "<img src="test.jpg" />" - No match
All matches found: 3
Matches: [('p', '', 'This is a paragraph.'), ('div', ' class="container"', 'Content here'), ('span', '', 'Text')]

07 Ip Address

Match valid IPv4 addresses with range validation.

Pattern breakdown:

  • 25[0-5] - 250-255
  • 2[0-4][0-9] - 200-249
  • [01]?[0-9][0-9]? - 0-199
  • Repeated 4 times with dots between

This validates that each octet is in the valid range 0-255.

Source: Regular-Expressions.info - IP Addresses

Pattern: \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
Test Input
Server IP: 192.168.1.1
Invalid: 256.1.1.1
Another valid: 10.0.0.255
Not an IP: 1.2.3
Match Results
Line 1: "Server IP: 192.168.1.1"
Match 1: "192.168.1.1"
Line 2: "Invalid: 256.1.1.1" - No match
Line 3: "Another valid: 10.0.0.255"
Match 1: "10.0.0.255"
Line 4: "Not an IP: 1.2.3" - No match
All matches found: 2
Matches: ['192.168.1.1', '10.0.0.255']

08 Credit Card

Match credit card numbers in various formats.

Pattern breakdown:

  • (?:\d{4}[-\s]?){3} - three groups of 4 digits with optional separator
  • \d{4} - final 4 digits

Security Note: Never log or store credit card numbers in plain text! This is for format validation only.

Source: Regular-Expressions.info - Credit Card Numbers

Pattern: \b(?:\d{4}[-\s]?){3}\d{4}\b
Test Input
Card: 1234-5678-9012-3456
Also valid: 1234 5678 9012 3456
No spaces: 1234567890123456
Invalid: 1234-5678-901
Match Results
Line 1: "Card: 1234-5678-9012-3456"
Match 1: "1234-5678-9012-3456"
Line 2: "Also valid: 1234 5678 9012 3456"
Match 1: "1234 5678 9012 3456"
Line 3: "No spaces: 1234567890123456"
Match 1: "1234567890123456"
Line 4: "Invalid: 1234-5678-901" - No match
All matches found: 3
Matches: ['1234-5678-9012-3456', '1234 5678 9012 3456', '1234567890123456']