Regex Tester - Test Regular Expressions
Test and debug regular expressions in real time. Enter a pattern and test string to see matches highlighted with capture group details.
Regular Expression Quick Reference
Regular expressions (regex) are patterns that describe sets of strings. They are supported in virtually every programming language and are invaluable for input validation, parsing, search-and-replace, and data extraction.
Character Classes
| Pattern | Matches |
|---|---|
. | Any character except newline |
\d | Any digit (0–9) |
\D | Any non-digit |
\w | Word character (a-z, A-Z, 0-9, _) |
\W | Non-word character |
\s | Whitespace (space, tab, newline) |
\S | Non-whitespace |
[abc] | One of a, b, or c |
[^abc] | Anything except a, b, or c |
[a-z] | Lowercase letter a through z |
Quantifiers
| Pattern | Meaning |
|---|---|
* | Zero or more (greedy) |
+ | One or more (greedy) |
? | Zero or one (greedy) |
*? | Zero or more (lazy) |
+? | One or more (lazy) |
{n} | Exactly n times |
{n,m} | Between n and m times |
Anchors and Groups
| Pattern | Meaning |
|---|---|
^ | Start of string (or line in multiline mode) |
$ | End of string (or line in multiline mode) |
\b | Word boundary |
(abc) | Capturing group |
(?:abc) | Non-capturing group |
(?<name>abc) | Named capturing group |
a|b | Alternation — matches a or b |
Common Regex Patterns
- Email validation:
^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$ - URL:
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b - IPv4 address:
^(\d{1,3}\.){3}\d{1,3}$ - Date (YYYY-MM-DD):
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$ - Hex color:
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$ - Strong password:
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Regex in Popular Languages
JavaScript
const pattern = /(\d{4})-(\d{2})-(\d{2})/g;
const matches = [...text.matchAll(pattern)];
// Named groups: /(?<year>\d{4})-(?<month>\d{2})/ Python
import re
pattern = re.compile(r'(\d{4})-(\d{2})-(\d{2})')
match = pattern.search(text)
if match:
year, month, day = match.groups() Tips for Writing Better Regex
- Be as specific as possible — overly broad patterns cause false positives and can be exploited via ReDoS (catastrophic backtracking).
- Use non-capturing groups
(?:...)when you do not need to reference the group later. - Anchor patterns with
^and$when validating whole strings, not just checking for a substring. - Test with edge cases: empty strings, Unicode characters, very long inputs, and inputs designed to cause backtracking.
- Add comments using
(?x)verbose mode (Python, .NET) for complex patterns.
Frequently Asked Questions
What regex flags are supported?
Common flags: g (global — find all matches), i (case-insensitive), m (multiline — ^ and $ match line boundaries), s (dotAll — dot matches newlines), and u (Unicode mode). Flags can be combined, e.g. /pattern/gi.
How do I match a literal dot, parenthesis, or other special character?
Escape special regex characters with a backslash: \. matches a literal dot, \( matches a literal parenthesis. The characters requiring escaping are: . * + ? ^ $ { } [ ] | ( ) \
What is the difference between greedy and lazy matching?
Greedy quantifiers (*, +) match as much text as possible. Lazy quantifiers (*?, +?) match as little as possible. For <.+> on <a><b>, greedy matches the whole string; lazy <.+?> matches <a> and <b> separately.