Certain regex patterns come up again and again in software development. Having a reliable set of go-to patterns saves time and reduces errors across web development, backend systems, and data processing.
1. Email Address
`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$` handles the vast majority of real-world addresses. Not fully RFC-compliant but sufficient for form validation.
2. URL
`^https?://[a-zA-Z0-9.-]+(?:/[a-zA-Z0-9._~:/?#@!$&'()*+,;=-]*)?$` matches HTTP/HTTPS URLs with optional paths. For strict validation, use a dedicated URL parser.
3. IPv4 Address
`^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$` validates each octet is between 0 and 255.
4. Date (YYYY-MM-DD)
`^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$` validates ISO 8601 format with basic range checking.
5. Strong Password
`^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$` uses lookaheads for multiple requirements: lowercase, uppercase, digit, special character, and minimum length.
6. HTML Tags
`<([a-zA-Z][a-zA-Z0-9]*)\b[^>]*>(.*?)</\1>` matches paired tags using a backreference. Note: use a DOM parser for full HTML parsing.
7. Whitespace Cleanup
`\s{2,}` (replace with single space) normalizes multiple whitespace characters. Combine with `^\s+|\s+$` for trimming.
8. Log Timestamp
`\[?(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?)\]?` extracts ISO 8601 timestamps from log lines.
Building Your Pattern Library
Test patterns against your actual data before deployment. RegExpress serves as both a testing environment and reference tool, letting you experiment and adapt patterns to your specific requirements.