Regular Expressions: A Practical Guide for Developers
8 min read · Updated June 2026
Regular expressions (regex) are a powerful pattern-matching language used in every programming language, text editor, and search tool. They can look intimidating, but once you understand the building blocks, you can read and write any regex.
The Building Blocks
Character Classes
| Pattern | Matches |
|---|---|
| . | Any character except newline |
| \d | Digit [0-9] |
| \w | Word character [a-zA-Z0-9_] |
| \s | Whitespace (space, tab, newline) |
| [abc] | Any of a, b, or c |
| [^abc] | Anything except a, b, or c |
Quantifiers
| Pattern | Matches | Type |
|---|---|---|
| * | 0 or more | Greedy |
| + | 1 or more | Greedy |
| ? | 0 or 1 | Greedy |
| {n,m} | Between n and m | Greedy |
| *? | 0 or more | Lazy |
| +? | 1 or more | Lazy |
Groups and References
(abc)— Capturing group (can be referenced as $1 or \1)(?:abc)— Non-capturing group (doesn't create a reference)(?<name>abc)— Named capturing groupabc|def— Alternation (matches abc OR def)
Flags
| Flag | Name | Effect |
|---|---|---|
| g | Global | Find all matches, not just the first |
| i | Case-insensitive | Match regardless of case |
| m | Multiline | ^ and $ match each line, not just the whole string |
| s | Dotall | . matches newlines too |
| u | Unicode | Enable Unicode property escapes (\p{L}) |
| y | Sticky | Match only at lastIndex position |
Common Patterns You'll Use
- Email:
/^[^\s@]+@[^\s@]+\.[^\s@]+$/ - URL:
/https?:\/\/(www\.)?[\w-]+\.[\w]{2,}(\/[\w-]*)*$/ - Phone (US):
/\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/ - IPv4:
/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b/ - Date (YYYY-MM-DD):
/\d{4}-\d{2}-\d{2}/
🧪 Test your Regex Live
Use our Regex Tester & Highlighter to test any pattern with real-time match highlighting, capture group display, and all flag support.
The Bottom Line
- Regex is built from character classes, quantifiers, groups, and anchors
- Use lazy quantifiers (*? +?) to avoid over-matching
- Named groups (
?<name>) make complex regex readable - Always test with edge cases — regex bugs are subtle
- For complex parsing (HTML, CSV), use a proper parser, not regex
Disclaimer: This guide is for informational purposes only. Regex implementations vary slightly between languages — always test in your target environment.