How to Write Regular Expressions — Free Guide
Writing regex patterns becomes straightforward once you have a systematic approach. Instead of guessing at syntax, you can build patterns piece by piece, test each addition, and arrive at exactly the match you need. This guide walks you through the process from blank pattern to working regex, with practical examples you can test in freeregextester.app.
How do I write a regex pattern from scratch?
Start with the concrete. Look at the strings you want to match and identify the parts that stay the same versus the parts that change. The static parts become literal characters in your pattern. The variable parts become regex syntax.
Here is a step-by-step example. Suppose you want to match phone numbers like 555-123-4567:
- The format is three digits, a dash, three digits, a dash, four digits
- Write the digit parts:
\d{3}for three digits,\d{4}for four - Add the literal dashes:
\d{3}-\d{3}-\d{4} - Test it in freeregextester.app with sample phone numbers
Test String Call 555-123-4567 or 800-555-9012 for info
What are the most common regex patterns?
These patterns cover the vast majority of everyday regex tasks. Bookmark this table or test each one in freeregextester.app to see them in action.
| Use Case | Pattern | Example Match |
|---|---|---|
| Numbers | \d+ |
42, 1000, 7 |
[\w.-]+@[\w.-]+\.\w{2,} |
user@example.com | |
| URL | https?://\S+ |
https://example.com/path |
| Whole word | \bword\b |
"word" but not "swordfish" |
| IP address | \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} |
192.168.1.1 |
| Date (YYYY-MM-DD) | \d{4}-\d{2}-\d{2} |
2026-04-09 |
| Hex color | #[0-9a-fA-F]{6}\b |
#C49A2A |
| Trim whitespace | ^\s+|\s+$ |
Leading/trailing spaces |
How do I use capture groups in regex?
Capture groups let you extract specific parts of a match. Wrap any part of your pattern in parentheses, and the regex engine will save that sub-match separately. This is essential for parsing structured text.
Test String Date: 2026-04-09
Group 1: 2026 Group 2: 04 Group 3: 09
You can also use named capture groups for clarity: (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2}). This makes your code more readable when you access the captured values. Test capture groups visually in freeregextester.app to verify they grab the right substrings.
How do I debug regex that isn't matching?
When your regex does not match what you expect, follow this debugging process:
- Simplify: Strip your pattern down to the most basic version and verify it matches. Then add pieces back one at a time.
- Check escaping: Characters like
.,(,),[,{,*,+,?,^,$, and\have special meaning. If you want a literal match, escape with backslash. - Check quantifiers:
*matches zero or more (including nothing).+requires at least one.?makes the preceding element optional. - Check anchors: If you use
^and$, make sure your flags include multiline if testing multi-line strings. - Check greedy vs lazy: If your pattern matches too much text, try adding
?after quantifiers to make them lazy.
Using freeregextester.app to debug is the fastest approach. You see matches update in real time as you modify the pattern, so you can isolate exactly where the issue lies.
What is the fastest way to test regex online?
freeregextester.app processes patterns instantly in your browser with zero server roundtrips. Open the app, type your regex, paste your test string, and see highlighted matches immediately. Because it runs locally, there is no latency even on slow connections, and it works offline as a PWA. No sign-up, no ads, no tracking.
Write and test regex now
Build patterns step by step with instant visual feedback in freeregextester.app.
Open Regex Tester