Learn regular expressions by seeing patterns, test cases, and match results side-by-side
Real-world regex patterns for common tasks
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.
user@example.com test.email+tag@domain.co.uk invalid@ @invalid.com valid_email123@test-domain.org
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 digitsThis handles formats like 555-123-4567, 555.123.4567, 5551234567, and 123-4567.
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.
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 queryVisit 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 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-31This validates month and day ranges (though doesn't check for valid days in each month).
Meeting on 2024-01-15 Deadline: 2024-12-31 Invalid: 2024-13-45 Also check 2023-06-30
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 totalThis ensures strong password requirements are met.
Password123! weakpass NoSpecial123 NoDigits! Short1! ValidPass123!
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 nameNote: This is simplified. Real HTML parsing should use proper HTML parsers, not regex.
<p>This is a paragraph.</p> <div class="container">Content here</div> <span>Text</span> <img src="test.jpg" />
Match valid IPv4 addresses with range validation.
Pattern breakdown:
25[0-5] - 250-2552[0-4][0-9] - 200-249[01]?[0-9][0-9]? - 0-199This validates that each octet is in the valid range 0-255.
Server IP: 192.168.1.1 Invalid: 256.1.1.1 Another valid: 10.0.0.255 Not an IP: 1.2.3
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 digitsSecurity Note: Never log or store credit card numbers in plain text! This is for format validation only.
Card: 1234-5678-9012-3456 Also valid: 1234 5678 9012 3456 No spaces: 1234567890123456 Invalid: 1234-5678-901