Regular expressions (regex) are powerful pattern-matching tools that every developer encounters. Here are the patterns you will use most often.
Email validation
^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$This is a simplified but practical email validator. True RFC 5321 compliance is significantly more complex, but this covers 99% of real-world cases.
URL matching
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)IP address (IPv4)
^((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]?)$Phone number (flexible)
^[+]?[0-9\s\-().]{7,20}$Strong password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$Requires at least one lowercase, one uppercase, one digit, and one special character with a minimum length of 8.
Hex color
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$Tips for writing regex
Always test your patterns against both positive and negative examples. Use named capture groups ((?<name>...)) for readability. Avoid catastrophic backtracking by being as specific as possible.