Regex Tester
Live regex match and highlight
About This Calculator
Regular expressions (regex) are patterns that describe sets of strings. They are used for text search and replace, input validation (email, phone, password formats), log parsing, and data extraction. A regex tester lets you test patterns against sample text interactively, showing matches highlighted in real time.
Formula
Basic syntax: . (any char), * (0+), + (1+), ? (0 or 1), ^ (start), $ (end)
Character classes: [abc] any of a,b,c; [^abc] not a,b,c; [a-z] range; \d digit; \w word char
Groups: (abc) capture group; (?:abc) non-capturing; (?=abc) lookahead
Flags: g=global (all matches), i=case insensitive, m=multiline
Example Calculation
Match all email addresses in text using regex
- Pattern: [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
- Input: 'Contact [email protected] or [email protected]'
- Matches: [email protected], [email protected]
Pattern matches both email addresses (2 matches found)
Common Regex Patterns
| Use Case | Pattern |
|---|---|
| Email address | [a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} |
| US phone number | \(?\d{3}\)?[-. ]?\d{3}[-. ]?\d{4} |
| URL | https?://[\w\-.]+(\.[a-z]{2,})+(/[\S]*)? |
| Date (YYYY-MM-DD) | \d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]) |
| IPv4 address | (\d{1,3}\.){3}\d{1,3} |
| ZIP code (US) | \d{5}(-\d{4})? |
Frequently Asked Questions
What is the difference between greedy and lazy matching?
Greedy quantifiers (*, +, {n,m}) match as much as possible. Lazy versions (*?, +?, {n,m}?) match as little as possible. For example, '<.+>' on '<a><b>' greedily matches '<a><b>' (entire string); '<.+?>' lazily matches '<a>' then '<b>' separately.
What are capture groups for?
Parentheses create capture groups that let you extract specific parts of a match. In 'Match (\w+)@(\w+)', group 1 captures the username and group 2 captures the domain. In most languages, you access them by index: match[1], match[2]. Named groups (?P<name>...) use names instead.
How do I match a literal dot or parenthesis?
Special regex characters (. * + ? ^ $ {} [] | () \) must be escaped with a backslash to match literally. To match a literal dot use \., to match a literal parenthesis use \( or \). For example, to match '3.14', use the pattern '3\.14' rather than '3.14' (which would also match '3X14').
What is the difference between regex flavors?
Different languages implement slightly different regex flavors: Python (re module), JavaScript, Java, PCRE (Perl Compatible), .NET. Core syntax is similar, but advanced features (lookbehind length limits, possessive quantifiers, atomic groups) vary. Most modern flavors support PCRE-like syntax.