Type a regex pattern and test string below. Matches are highlighted in real time.
Try it: Change the pattern above to test any regex from this cheat sheet. For a full-featured tester, use our Regex Tester tool.
Basics & Anchors
Pattern
Description
Example
.
Any character except newline
a.c matches "abc", "a1c"
^
Start of string (or line with m flag)
^Hello matches "Hello world"
$
End of string (or line with m flag)
end$ matches "the end"
\b
Word boundary
\bcat\b matches "cat" but not "catch"
\B
Non-word boundary
\Bcat matches "catch" but not "cat"
\
Escape special character
\. matches literal "."
|
Alternation (OR)
cat|dog matches "cat" or "dog"
Special characters that need escaping: . ^ $ * + ? { } [ ] \ | ( )
Character Classes
Pattern
Description
Equivalent
[abc]
Match any one of a, b, or c
[^abc]
Match anything except a, b, or c
[a-z]
Match any lowercase letter
[A-Z]
Match any uppercase letter
[0-9]
Match any digit
\d
[a-zA-Z0-9]
Match any alphanumeric character
\d
Digit
[0-9]
\D
Non-digit
[^0-9]
\w
Word character
[a-zA-Z0-9_]
\W
Non-word character
[^a-zA-Z0-9_]
\s
Whitespace (space, tab, newline)
[ \t\n\r\f\v]
\S
Non-whitespace
[^ \t\n\r\f\v]
Quantifiers
Pattern
Description
Example
*
0 or more (greedy)
ab*c matches "ac", "abc", "abbc"
+
1 or more (greedy)
ab+c matches "abc", "abbc" but not "ac"
?
0 or 1 (optional)
colou?r matches "color" and "colour"
{n}
Exactly n times
\d{4} matches "2026"
{n,}
n or more times
\d{2,} matches "42", "123", "9999"
{n,m}
Between n and m times
\d{2,4} matches "42", "123", "9999"
*?
0 or more (lazy / non-greedy)
".*?" matches first quoted string
+?
1 or more (lazy)
.+?; matches up to first semicolon
??
0 or 1 (lazy)
Greedy vs lazy: By default, quantifiers are greedy (match as much as possible). Adding ? after makes them lazy (match as little as possible). Example: for the string "a" and "b", the pattern ".*" matches the entire string, while ".*?" matches "a" then "b" separately.
Groups & Backreferences
Pattern
Description
Example
(abc)
Capturing group
(ha)+ matches "haha"
(?:abc)
Non-capturing group
(?:ha)+ matches "haha" (no capture)
(?<name>abc)
Named capturing group
(?<year>\d{4})
\1
Backreference to group 1
(ha)\1 matches "haha"
\k<name>
Backreference to named group
(?<word>\w+)\s\k<word> matches repeated words
(a|b)
Alternation within group
(cat|dog)s matches "cats" or "dogs"
Useful group patterns
Pattern
What it does
(\w+)\s\1
Find duplicate words ("the the")
(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})
Named capture for date parts
(["']).*?\1
Match quoted strings (matching quotes)
Lookahead & Lookbehind
Lookaround assertions match a position without consuming characters. They check what comes before or after the current position.
Pattern
Name
Description
(?=abc)
Positive lookahead
Followed by "abc"
(?!abc)
Negative lookahead
NOT followed by "abc"
(?<=abc)
Positive lookbehind
Preceded by "abc"
(?<!abc)
Negative lookbehind
NOT preceded by "abc"
Practical examples
Pattern
What it matches
\d+(?= dollars)
Numbers followed by " dollars" (matches "100" in "100 dollars")
\d+(?! dollars)
Numbers NOT followed by " dollars"
(?<=\$)\d+
Numbers preceded by "$" (matches "50" in "$50")
(?<!@)\w+
Words NOT preceded by "@"
(?=.*[A-Z])(?=.*\d).{8,}
Password with at least 1 uppercase and 1 digit, 8+ chars
Note: Lookbehind has limited support in some engines. JavaScript supports it in modern browsers (ES2018+). Python, Java, .NET, and PCRE all support it.