Common Regex Patterns

Regular Expression Patterns for Common Tasks

Phone Numbers

US validator
(?:\+?(1)|(1?))[\s_.-]?\(?(\d{3})\)?[\s_.-]?(\d{3})[\s_.-]?(\d{4})
Matches US 10 and 11 digit numbers with an optional + prefix when followed by a 1. Groups capture the actual digits. Allows for differing separators.
US validator (more strict)
(?:\+?(1)|(1?))([\s_.-]?)\(?(\d{3})\)?\3(\d{3})\3(\d{4})
Matches US 10 and 11 digit numbers with an optional + prefix when followed by a 1. Groups capture the actual digits. Forces the same separator character between each digit group.

US Addresses

US ZIP code validator
((\d{5})(?:-(\d{4}))?)
Matches US 5 and 9 digit ZIP codes. Groups capture the entire 9 digits and hyphen, as well as the separate number groups.
US state code validator (2 char)
(A[AELKSZRP]|C[AOT]|D[EC]|F[ML]|G[AU]|HI|I[DLNA]|K[SY]|LA|M[EHDAINSOTP]|N[EVHJMYCD]|O[HKR]|P[WAR]|RI|S[CD]|T[NX]|UT|V[TIA]|W[AVIY])
Matches US state codes, consisting of specific 2 character upper-case letters. Group captures the entire 2 character code.

IP Addresses

Basic validation
(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)
This will validate IP addresses of the form 0-255.0-255.0-255.0-255. Groups capture each octet.

Email

Very basic validation
([\w\d._%+]+)@([\w\d.]+\.[a-zA-Z]{2,4})
This is quite simple and will validate basic email addresses. It's not great though and has some obvious issues. For instance, it would match the string .@..com.
Basic validation
([\w\d_%+]+(?:\.[\w\d_%+]+)*)@((?:[\w\d_]+\.)*[\w\d]+\.[A-Za-z]{2,63})
This works fairly well for the vast majority of email addresses. It's not perfect, but will fit the vast majority of email address out there without being unreasonably large and complicated. To get a better idea of why one might make a tradeoff, check out the full RFC compliant email validation pattern below!
Full validation
Click here to view pattern
This pattern is so big I can't even fit it here. It does properly validate all email (as far as I know), at the cost of being unwieldy. It may be more efficient to parse using methods other than Regex.

Number Bases

Hexadecimal byte string validator
(?:0x)?((?:[a-fA-F\d]{2})+)
Finds strings with or without 0x prefix. Ensures full bytes (double hex characters).
Hexadecimal color validator
(?:#?([a-fA-F\d]{6}|[a-fA-F\d]{3}))
Finds strings with or without # prefix. Ensures 3 or 6 nibbles (#FFF or #FFFFFF).

HTML

Fairly basic tag parsing
<(\w+)(?:\s(\w+)="?([^"]+)")*>([^<]*)</\1>
Matches paired and un-nested HTML tags. Groups capture the tag name, last attribute name and value, and the content of the tag body.