Regular Expression Patterns for Common Tasks
- US validator
- 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)
- 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 ZIP code validator
- 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)
- Matches US state codes, consisting of specific 2 character upper-case letters. Group captures the entire 2 character code.
- Basic validation
- This will validate IP addresses of the form 0-255.0-255.0-255.0-255. Groups capture each octet.
- Very basic validation
- 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
- 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.
- Hexadecimal byte string validator
- Finds strings with or without 0x prefix. Ensures full bytes (double hex characters).
- Hexadecimal color validator
- Finds strings with or without # prefix. Ensures 3 or 6 nibbles (#FFF or #FFFFFF).
- Fairly basic tag parsing
- Matches paired and un-nested HTML tags. Groups capture the tag name, last attribute name and value, and the content of the tag body.