10 Practical Regular Expression Examples You Need Regular expressions (regex) are powerful tools for pattern matching and text manipulation. They save time during data validation, scraping, and cleaning. Here are 10 practical regex examples you can use in your daily development workflows. 1. Matching an Email Address Validating email addresses is a common task for web forms. Regex: ^[a-zA-Z0-9.%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}\(</code></p> <p><strong>How it works:</strong> It checks for alphanumeric characters and common symbols before an <code>@</code> sign, followed by a domain name and a minimum two-letter top-level domain (like .com or .org). 2. Validating Phone Numbers (US Format)</p> <p>This pattern matches standard 10-digit North American phone numbers with optional formatting. <strong>Regex:</strong> <code>^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})\)
How it works: It accommodates optional parentheses around the area code, as well as optional hyphens, periods, or spaces separating the number blocks. 3. Extracting URLs
Use this pattern to find or validate web links within a block of text.
Regex: https?:\/\/(www.)?[-a-zA-Z0-9@:%.+#=]{1,256}.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.#?&//=])
How it works: It searches for http or https, handles optional www., and captures the domain along with any trailing URL paths or query parameters. 4. Matching ISO Dates (YYYY-MM-DD)
This pattern validates dates formatted under the international standard. Regex: ^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$
How it works: It enforces a four-digit year, a two-digit month between 01 and 12, and a two-digit day between 01 and 31. 5. Strong Password Validation Ensure user passwords meet strict security requirements.
Regex: ^(?=.[a-z])(?=.[A-Z])(?=.\d)(?=.[@$!%?&])[A-Za-z\d@\(!%*?&]{8,}\)
How it works: It utilizes positive lookaheads to enforce at least one lowercase letter, one uppercase letter, one number, one special character, and a minimum length of 8 characters. 6. Finding Duplicate Words
Clean up prose or text logs by catching accidental repeated words. Regex: \b(\w+)\s+\1\b
How it works: It captures a word block using (\w+) and checks if the immediate next word \1 matches the exact same captured group. 7. Verifying Hex Color Codes Validate CSS hex color values in web designs. Regex: ^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\(</code></p> <p><strong>How it works:</strong> It matches a leading <code>#</code> symbol followed by either exactly 3 or exactly 6 valid hexadecimal characters (0-9 and A-F). 8. Extracting HTML/XML Tags</p> <p>Isolate or remove specific markup elements from source text. <strong>Regex:</strong> <code><([^>]+)></code></p> <p><strong>How it works:</strong> It matches an opening angle bracket, captures everything that is not a closing bracket, and terminates at the closing angle bracket. 9. Validating IPv4 Addresses Ensure network IP strings conform to standard notation.</p> <p><strong>Regex:</strong> <code>^((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]?)\)
How it works: It limits each of the four numeric blocks to a range between 0 and 255, separated by periods. 10. Stripping Whitespace
Clean up strings by identifying unnecessary leading or trailing spaces. Regex: ^\s+|\s+$
How it works: It looks for one or more spaces at the absolute beginning of a string or at the absolute end, making it ideal for string trimming functions.
Leave a Reply