You're parsing log files. The pattern looks right: \d{4}-\d{2}-\d{2}. You test it in your code—nothing matches. You stare at the regex. You stare at the data. You add a flag. Still nothing. You try escaping a character. Now it matches too much. Twenty minutes later, you discover the issue: your language uses different syntax for capture groups. The regex was fine. The translation wasn't.
Writing regex is one skill. Knowing it works before it hits production is another. Test first, debug never.
Regular expressions are powerful but unforgiving. One wrong character and your pattern matches everything—or nothing. This guide covers everything you need to know about testing, debugging, and mastering regex.
What Are Regular Expressions?
Regular expressions (regex) are patterns that describe text. They're used for:
- Validation - Email addresses, phone numbers, passwords
- Search - Finding patterns in logs, documents, code
- Extraction - Pulling data from unstructured text
- Replacement - Find-and-replace with pattern matching
Every major programming language supports regex, but syntax varies. A pattern that works in JavaScript might need adjustment for Python or Java.
Basic Regex Syntax
\d Match any digit (0-9)
\w Match any word character (a-z, A-Z, 0-9, _)
\s Match any whitespace
. Match any character except newline
* Zero or more of the previous
+ One or more of the previous
? Zero or one of the previous
^ Start of string
$ End of string
How to Use Our Regex Tester
Our free Regex Tester provides real-time pattern matching with instant visual feedback.
Step-by-Step Guide
- Navigate to the Regex Tester
- Enter your regex pattern
- Set flags (global, case-insensitive, multiline, etc.)
- Paste your test string
- See matches highlighted instantly
- Export working code to your language
Features
- Real-time matching - See results as you type
- Match highlighting - Visual feedback on what's captured
- Capture groups - View named and numbered groups
- Replace mode - Test find-and-replace operations
- Diff view - Compare before/after replacements
- 8-language export - Generate working code instantly
- Pattern explanation - Understand what each part does
- 20 common patterns - Email, URL, phone, IP, date, and more
- Cheat sheet - Quick reference while you work
- URL sharing - Share patterns with teammates
- Privacy-focused - All processing happens in your browser
Common Regex Patterns
These patterns handle the most frequent validation tasks. Select them directly in our Regex Tester or copy them here.
Email Address
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Matches: [email protected], [email protected]
URL
https?:\/\/[^\s]+
Matches: https://example.com, http://sub.domain.org/path?query=1
Phone Number (US)
(\+1[-.\s]?)?(\(?\d{3}\)?[-.\s]?)?\d{3}[-.\s]?\d{4}
Matches: 555-123-4567, (555) 123-4567, +1 555.123.4567
Phone Number (International)
\+[1-9]\d{1,14}
Matches: +14155551234, +442071234567 (E.164 format)
IPv4 Address
\b(?:(?: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]?)\b
Matches: 192.168.1.1, 10.0.0.255
Date (ISO Format)
\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])
Matches: 2024-01-15, 2023-12-31
Hex Color Code
#(?:[0-9a-fA-F]{3}){1,2}\b
Matches: #FFF, #a1b2c3, #FF5733
UUID
[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}
Matches: 550e8400-e29b-41d4-a716-446655440000
Username
^[a-zA-Z][a-zA-Z0-9_]{2,15}$
Matches: john_doe, User123 (3-16 chars, starts with letter)
Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Requires: lowercase, uppercase, digit, special character, 8+ length
Regex Cheat Sheet
Character Classes
| Pattern | Description | Example |
|---|---|---|
. | Any character except newline | a.c matches abc, a1c |
\d | Any digit (0-9) | \d\d matches 42 |
\D | Any non-digit | \D+ matches abc |
\w | Word character (a-z, A-Z, 0-9, _) | \w+ matches hello_123 |
\W | Non-word character | \W matches @, # |
\s | Whitespace (space, tab, newline) | a\sb matches a b |
\S | Non-whitespace | \S+ matches hello |
[abc] | Any character in set | [aeiou] matches vowels |
[^abc] | Any character NOT in set | [^0-9] matches non-digits |
[a-z] | Character range | [A-Za-z] matches letters |
Quantifiers
| Pattern | Description | Example |
|---|---|---|
* | Zero or more | ab*c matches ac, abc, abbc |
+ | One or more | ab+c matches abc, abbc |
? | Zero or one (optional) | colou?r matches color, colour |
{n} | Exactly n times | \d{4} matches 2024 |
{n,} | n or more times | \d{2,} matches 12, 123, 1234 |
{n,m} | Between n and m times | \d{2,4} matches 12, 123, 1234 |
*? | Lazy zero or more | <.*?> matches single tags |
+? | Lazy one or more | ".+?" matches quoted strings |
Anchors
| Pattern | Description | Example |
|---|---|---|
^ | Start of string/line | ^Hello matches Hello world |
$ | End of string/line | world$ matches Hello world |
\b | Word boundary | \bcat\b matches cat not category |
\B | Non-word boundary | \Bcat matches category not cat |
Groups and References
| Pattern | Description | Example |
|---|---|---|
(abc) | Capture group | (ab)+ matches abab |
(?:abc) | Non-capturing group | (?:ab)+ groups without capturing |
(?<name>abc) | Named capture group | (?<year>\d{4}) captures as "year" |
\1 | Backreference to group 1 | (\w+)\s+\1 matches the the |
a|b | Alternation (or) | cat|dog matches cat or dog |
Lookahead and Lookbehind
| Pattern | Description | Example |
|---|---|---|
(?=abc) | Positive lookahead | \d+(?=px) matches 10 in 10px |
(?!abc) | Negative lookahead | \d+(?!px) matches 10 in 10em |
(?<=abc) | Positive lookbehind | (?<=\$)\d+ matches 50 in $50 |
(?<!abc) | Negative lookbehind | (?<!\$)\d+ matches 50 in €50 |
Export to Code
Our Regex Tester generates working code in 8 languages. No more syntax translation errors.
JavaScript
const regex = /\d{4}-\d{2}-\d{2}/g;
const matches = text.match(regex);
Python
import re
pattern = '\\d{4}-\\d{2}-\\d{2}'
matches = re.findall(pattern, text)
Java
import java.util.regex.*;
Pattern pattern = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
C#
using System.Text.RegularExpressions;
var regex = new Regex(@"\d{4}-\d{2}-\d{2}");
var matches = regex.Matches(text);
PHP
$pattern = '/\d{4}-\d{2}-\d{2}/';
preg_match_all($pattern, $text, $matches);
Go
import "regexp"
re := regexp.MustCompile(`\d{4}-\d{2}-\d{2}`)
matches := re.FindAllString(text, -1)
Ruby
pattern = /\d{4}-\d{2}-\d{2}/
matches = text.scan(pattern)
Bash
# Using grep -E (extended regex)
printf '%s\n' "$text" | grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2}'
Why Export Matters
Each language handles regex differently:
| Language | Delimiter | Escaping | Flags |
|---|---|---|---|
| JavaScript | /pattern/ | Single \ | After closing / |
| Python | String | Double \\ | Separate argument |
| Java | String | Double \\ | Pattern.CONSTANT |
| C# | @"pattern" | Single \ | RegexOptions.Flag |
| PHP | '/pattern/' | Single \ | After closing / |
| Go | Backticks | Single \ | Inline (?i) |
| Ruby | /pattern/ | Single \ | After closing / |
| Bash/grep | Quoted | Varies | Command flags |
Our exporter handles these differences automatically. Test once, export everywhere.
Common Regex Mistakes
Mistake 1: Forgetting to Escape Special Characters
Problem:
example.com
The . matches ANY character, so this matches exampleXcom, example-com, etc.
Solution:
example\.com
Special characters that need escaping: . * + ? ^ $ { } [ ] \ | ( )
Mistake 2: Greedy vs Lazy Quantifiers
Problem:
<.*>
Given <div>Hello</div>, this matches the entire string, not just <div>.
Solution:
<.*?>
The ? makes * lazy—it matches as little as possible.
Mistake 3: Not Anchoring Patterns
Problem:
\d{3}
This matches 123 inside abc123def456 (multiple times).
Solution:
^\d{3}$
Anchors ensure the entire string is exactly 3 digits.
Mistake 4: Overly Permissive Patterns
Problem:
.*@.*\..*
This "email validation" matches @@., [email protected], and countless invalid strings.
Solution:
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Be specific about what characters are allowed where.
Mistake 5: Catastrophic Backtracking
Problem:
(a+)+b
On input aaaaaaaaaaaaaaaaaaaaaaaaaaac, this can take exponential time.
Solution:
a+b
Avoid nested quantifiers. Our tester warns you about dangerous patterns.
Mistake 6: Forgetting Multiline Mode
Problem:
^Error:.*$
Without multiline mode, ^ and $ only match start/end of the entire string.
Solution:
Enable the multiline flag (m) to match start/end of each line.
Performance Tips
Regular expressions can be slow. Here's how to keep them fast.
Avoid Catastrophic Backtracking
These patterns are dangerous:
(a+)+ # Nested quantifiers
(a|a)+ # Overlapping alternations
.*.* # Multiple greedy quantifiers
Our Regex Tester detects these patterns and shows warnings.
Be Specific
Specific patterns are faster than general ones:
# Slow - matches too much, backtracks heavily
.*error.*
# Fast - more specific character class
[^\n]*error[^\n]*
Use Non-Capturing Groups
If you don't need the captured value, use (?:...):
# Captures (slower, uses memory)
(https?):\/\/(.*)
# Non-capturing (faster)
(?:https?):\/\/(.*)
Anchor When Possible
Anchored patterns fail faster on non-matching input:
# Must scan entire string
\d{4}-\d{2}-\d{2}
# Fails immediately if start doesn't match
^\d{4}-\d{2}-\d{2}
Test with Real Data
Always test with:
- Valid inputs - Ensure matches work
- Invalid inputs - Ensure non-matches are fast
- Edge cases - Empty strings, very long strings, special characters
Advanced Features
Named Capture Groups
Instead of numbered groups, use names:
(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
Access in JavaScript:
const match = regex.exec(text);
console.log(match.groups.year); // "2024"
Lookahead for Validation
Validate password requirements without consuming characters:
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).{8,}$
This checks for uppercase, lowercase, and digit anywhere in the string.
Lookbehind for Extraction
Extract values after specific prefixes:
(?<=price:\s*)\d+
Matches 100 in price: 100 without including price: .
Alternation with Lookarounds
Combine lookarounds with alternation to match context-dependent patterns:
(?<=\$)\d+|\d+(?=€)
Matches numbers after $ (like 50 in $50) or before € (like 50 in 50€).
Debugging Regex
When a pattern doesn't work:
- Simplify - Start with the smallest matching pattern
- Build up - Add complexity one piece at a time
- Test each part - Use our explanation feature to understand each segment
- Check flags - Global, multiline, and case-insensitive flags change behavior
- Verify escaping - Special characters need backslashes
- Try the common patterns - Start from a working template
Our Regex Tester shows exactly what each part of your pattern does, making debugging straightforward.
Regex Flags Explained
| Flag | Name | Effect |
|---|---|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | A matches a |
m | Multiline | ^ and $ match line boundaries |
s | Dotall | . matches newlines too |
u | Unicode | Enable full Unicode support |
y | Sticky | Match only at lastIndex position |
Conclusion
The regex that works is the regex you tested. The regex you didn't test is the bug you'll find in production.
Regular expressions are powerful but demand precision. Our Regex Tester lets you build patterns visually, see exactly what matches, and export working code to any language—all before your pattern touches real data.
Stop guessing whether your regex works. Paste it in, test it, export it, ship it. The tool catches the mistakes so your code doesn't have to.
Keep Reading
- JSON Formatter & Validator Guide - Another essential developer tool
- Essential Developer Utilities Guide - Complete your toolkit
- Data Backup Security Checklist - Protect your work
Related Tools
- Regex Tester - Test and debug regular expressions online
- JSON Formatter - Format and validate JSON