Regular Expressions Made Easy: 10 Practical Regex Examples
Regular expressions have a reputation for being cryptic and impenetrable. But the truth is, a small set of patterns handles the vast majority of real-world text processing tasks. Whether you are validating user input, parsing logs, or extracting data from strings, these ten regex example patterns will cover most of what you need — and a good regex tester makes building and debugging them painless.
All patterns below are tested with the DevKitDock Regex Tester, which highlights matches in real time and shows capture groups.
Regex Fundamentals: The Building Blocks
Before diving into examples, here are the essential tokens every developer should know:
| Token | Meaning | Example |
|---|---|---|
. |
Any character (except newline) | a.c matches "abc", "a1c", "a c" |
\d |
Digit (0–9) | \d\d matches "42" |
\w |
Word character (letter, digit, underscore) | \w+ matches "hello_123" |
\s |
Whitespace | hello\sworld matches "hello world" |
* |
Zero or more | ab*c matches "ac", "abc", "abbc" |
+ |
One or more | ab+c matches "abc", "abbc" but not "ac" |
? |
Zero or one (optional) | colou?r matches "color" and "colour" |
{n,m} |
Between n and m times | \d{2,4} matches "42", "2026" |
[abc] |
Character set | [aeiou] matches any vowel |
(...) |
Capture group | (\d+)-(\d+) captures each number |
^ / $ |
Start / end of string | ^Hello matches only at the start |
10 Practical Regex Examples
1. Email Address
A practical (not RFC-5322-perfect) email pattern that catches 99% of real addresses:
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
✓ user@example.com
✓ first.last+tag@sub.domain.co.uk
✗ @missing-local.com
✗ no-at-sign.com
Perfect email validation is surprisingly complex — the full RFC 5322 spec allows quoted strings, comments, and IP address literals. For form validation, a simple pattern plus a confirmation email is more practical than a bulletproof regex.
2. Phone Numbers (US Format)
^\+?1?\s*\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
✓ (555) 123-4567
✓ +1 555.123.4567
✓ 5551234567
✗ 123-45
✗ abc-def-ghij
3. URLs
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
✓ https://example.com/path?q=1
✓ http://sub.domain.org
✗ ftp://not-http.com
✗ not-a-url
4. IPv4 Addresses
\b((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)\b
✓ 192.168.1.1
✓ 255.255.255.0
✓ 0.0.0.0
✗ 256.1.1.1
✗ 999.999.999.999
5. Dates (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
✓ 2026-08-19
✓ 2000-01-01
✗ 2026-13-01
✗ 2026-00-15
6. HTML Tags
<\/?[a-z][a-z0-9]*[^>]*>
✓ <div class="container">
✓ </p>
✓ <br />
✗ not a tag
Note: for parsing structured HTML, always use a proper HTML parser. Regex is useful for quick extraction and pattern matching in logs or simple templates, but it cannot handle nested HTML reliably.
7. Hex Color Codes
#([0-9a-fA-F]{3}){1,2}\b
✓ #FFF
✓ #ff5733
✓ #000
✗ #GGG
✗ #12345
8. Password Strength
A password that requires at least 8 characters, one uppercase, one lowercase, one digit, and one special character:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
✓ Str0ng!Pass
✓ MyP@ssw0rd
✗ weak
✗ NoNumbers!
9. Extracting File Extensions
\.([a-zA-Z0-9]+)$
Captures: "js" from "app.js", "html" from "index.html"
10. Comma-Separated Values
[^,\s]+
From "apple, banana, cherry" matches: "apple", "banana", "cherry"
A more robust CSV pattern that handles quoted fields:
(?:"([^"]*)"|([^,]*))(?:,|$)
From '"hello, world",foo,bar' captures: "hello, world", "foo", "bar"
Regex Tips for Real-World Use
Use Non-Capturing Groups When You Do Not Need the Match
(?:https?:\/\/)?www\.\d+ // (?:...) does not capture, saving memory
Be Specific with Quantifiers
Prefer \d{4} over \d+ when you know the exact length. Specific patterns are faster, produce fewer false positives, and make your intent clearer.
Use Anchors to Avoid Partial Matches
\d+ → matches "123" inside "abc123def"
^\d+$ → matches only if the entire string is digits
Test Edge Cases
Empty strings, strings with only whitespace, very long strings, and strings with special characters often break poorly written patterns. Use a regex tester to validate against these edge cases before deploying.
Common Regex Flags
| Flag | Name | Effect |
|---|---|---|
g |
Global | Find all matches, not just the first |
i |
Case-insensitive | Ignore case when matching |
m |
Multiline | ^ and $ match line boundaries |
s |
Dotall | . matches newline characters too |
Flags are appended after the closing slash: /pattern/gi. In JavaScript, you can also pass them as the second argument: new RegExp('pattern', 'gi').
Frequently Asked Questions
What is a regex tester and why should I use one?
A regex tester is an interactive tool that lets you write a regular expression, test it against sample text, and see matches highlighted in real time. It saves significant time compared to writing regex blind — you see exactly what matches and what does not, including capture groups. The DevKitDock Regex Tester runs entirely in your browser.
Can regular expressions parse HTML?
Regex can extract simple patterns from HTML (like finding all href values), but it cannot reliably parse nested HTML. For structured HTML parsing, use a DOM parser like DOMParser in the browser or BeautifulSoup in Python. Regex works well for flat patterns, not recursive structures.
What is the difference between greedy and lazy matching?
Greedy quantifiers (*, +, ?) match as much as possible. Lazy quantifiers (*?, +?, ??) match as little as possible. For example, <.+> matches everything from the first < to the last >, while <.+?> matches individual tags.
How do I match special characters like dots and brackets?
Escape them with a backslash: \. matches a literal dot, \[ matches a literal bracket, \\ matches a literal backslash. Inside a character class [...], most special characters lose their special meaning, but ], \, ^, and - still need escaping.
Why is my regex slow or causing timeouts?
Regex performance degrades with catastrophic backtracking — patterns like (a+)+b on non-matching input cause exponential time complexity. Avoid nested quantifiers, use atomic groups or possessive quantifiers where available, and always test regex patterns against the longest expected input with a regex tester.