Regex Match Highlighter
Test regex patterns and highlight all matches in your text.
Regular Expression Testing Guide
Regular expressions (regex) are powerful patterns for matching text. Whether you're validating input, searching logs, or parsing data, regex is an essential tool for developers, data analysts, and power users. This tool helps you test and visualize regex matches in real-time.
Common Regex Patterns
| Pattern | Description | Example |
|---|---|---|
\d |
Any digit (0-9) | Matches: 5, 9, 0 |
\w |
Word character (a-z, A-Z, 0-9, _) | Matches: a, Z, 5, _ |
\s |
Whitespace (space, tab, newline) | Matches: space, \t, \n |
\b |
Word boundary | Start/end of word |
. |
Any character (except newline) | Matches: a, 5, ! |
^ |
Start of string/line | Beginning anchor |
$ |
End of string/line | Ending anchor |
* |
0 or more times | a* matches "", "a", "aaa" |
+ |
1 or more times | a+ matches "a", "aaa" |
? |
0 or 1 time | a? matches "", "a" |
{n} |
Exactly n times | \d{3} matches "123" |
{n,m} |
Between n and m times | \d{2,4} matches "12", "1234" |
[abc] |
Character class (a or b or c) | Matches: a, b, c |
[^abc] |
Negated class (not a, b, or c) | Matches: d, e, f, etc. |
| |
Alternation (or) | cat|dog matches "cat" or "dog" |
() |
Capturing group | Capture matched text |
Practical Regex Examples
Email Address
\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
Matches: user@example.com, john.doe+filter@domain.co.uk
Phone Numbers
\d{3}-\d{3}-\d{4}
Matches: 123-456-7890
URL
https?://[^\s]+
Matches: http://example.com, https://site.com/path
IPv4 Address
\b(?:\d{1,3}\.){3}\d{1,3}\b
Matches: 192.168.1.1, 10.0.0.1
Hexadecimal Color
#[0-9A-Fa-f]{6}\b
Matches: #FF5733, #000000
Date (YYYY-MM-DD)
\d{4}-\d{2}-\d{2}
Matches: 2025-01-15, 2024-12-31
HTML Tag
<([a-z]+)([^<>]*)>.*?</\1>
Matches: <div>content</div>, <p class="text">text</p>
Regex Flags Explained
| Flag | Name | Description |
|---|---|---|
i |
Case Insensitive | Makes pattern match both uppercase and lowercase. /hello/i matches "Hello", "HELLO" |
m |
Multiline | Makes ^ and $ match line beginnings/ends, not just string start/end |
s |
Dotall | Makes . match newlines too (normally it doesn't) |
Capturing Groups
Parentheses create capturing groups that extract specific parts of matches:
(\d{3})-(\d{3})-(\d{4})
Applied to "123-456-7890":
- Group 1: 123
- Group 2: 456
- Group 3: 7890
Non-Capturing Groups
Use (?:...) to group without capturing:
(?:https?|ftp)://[^\s]+
Groups for alternation but doesn't capture "http" or "https"
Common Regex Pitfalls
- Greedy vs Lazy:
.*is greedy (matches as much as possible),.*?is lazy (matches as little as possible) - Escaping Special Characters: To match literal
.,*,+, etc., escape them:\.,\* - Performance: Complex patterns with many alternations or backtracking can be slow
- Unicode: Basic regex doesn't handle all Unicode characters. Consider using Unicode-aware patterns when needed
Testing Best Practices
- Start Simple: Build complex patterns incrementally, testing each addition
- Test Edge Cases: Try empty strings, special characters, very long strings
- Use Online Tools: Test your regex in multiple tools to ensure compatibility
- Document Complex Patterns: Add comments explaining what each part does
- Consider Alternatives: Sometimes string methods are simpler than regex
Real-World Applications
Log File Analysis
Extract error messages, timestamps, or specific events from logs using regex patterns.
Data Validation
Validate user input like emails, phone numbers, credit cards, postal codes, etc.
Text Processing
Find and replace patterns in documents, code refactoring, bulk text transformations.
Web Scraping
Extract specific data from HTML or text responses when parsing is overkill.
Search and Replace in IDEs
Most code editors support regex in find/replace, enabling powerful refactoring.
Quick Reference
Character Classes:
\d= digit\w= word char\s= whitespace.= any char
Quantifiers:
*= 0 or more+= 1 or more?= 0 or 1{n,m}= n to m times
Anchors: ^ start, $ end, \b word boundary
Common Patterns
Email:\b[\w.-]+@[\w.-]+\.\w+\b
URL:https?://[^\s]+
Phone:\d{3}-\d{3}-\d{4}
Hex Color:#[0-9A-Fa-f]{6}