Skip to main content
TellaDev
Cheatsheets Regular Expressions

35 entries

Regular Expressions

Regex syntax and patterns for matching, capturing, and transforming text

35 commands

.
Match any single character except a newline
a.c matches 'abc', 'aXc'
\d
Match any digit (0-9)
\d{3} matches '123'
\D
Match any non-digit character
\D+ matches 'abc'
\w
Match any word character (letters, digits, underscore)
\w+ matches 'hello_42'
\W
Match any non-word character
\W matches '@' in 'a@b'
\s
Match any whitespace character (space, tab, newline)
\s+ matches ' ' (spaces)
\S
Match any non-whitespace character
\S+ matches 'word'
*
Match the preceding element zero or more times (greedy)
ab*c matches 'ac', 'abbc'
+
Match the preceding element one or more times (greedy)
ab+c matches 'abc', 'abbc'
?
Match the preceding element zero or one time (optional)
colou?r matches 'color', 'colour'
{n}
Match exactly n occurrences of the preceding element
\d{4} matches '2024'
{n,m}
Match between n and m occurrences of the preceding element
\w{3,5} matches 'foo', 'hello'
*?
Match the preceding element zero or more times (lazy/non-greedy)
<.*?> matches '<b>' not '<b>text</b>'
(abc)
Capture group — matches 'abc' and stores it as a group
(\d+)-(\d+) captures '12-34'
(?:abc)
Non-capturing group — groups without storing the match
(?:http|https):// groups without capture
(?<name>abc)
Named capture group — accessible by name in the match result
(?<year>\d{4})-(?<month>\d{2})
(a|b)
Alternation — match either 'a' or 'b'
(cat|dog) matches 'cat' or 'dog'
\1
Backreference to the first capture group
(\w+)\s\1 matches 'the the'
^
Match the start of a string (or line in multiline mode)
^Hello matches 'Hello world'
$
Match the end of a string (or line in multiline mode)
end$ matches 'the end'
\b
Match a word boundary (between \w and \W)
\bword\b matches whole 'word'
\B
Match a position that is NOT a word boundary
\Bcat matches 'bobcat'
[abc]
Character class — match any one of 'a', 'b', or 'c'
[aeiou] matches any vowel
[^abc]
Negated character class — match any character NOT in the set
[^0-9] matches non-digits
[a-z]
Character range — match any lowercase letter
[a-z]+ matches 'hello'
[A-Za-z0-9]
Match any alphanumeric character (letters and digits)
[A-Za-z0-9_]+ for identifiers
(?=abc)
Positive lookahead — asserts 'abc' follows without consuming it
\d(?=px) matches '5' in '5px'
(?!abc)
Negative lookahead — asserts 'abc' does NOT follow
\d(?!px) matches '5' in '5em'
(?<=abc)
Positive lookbehind — asserts 'abc' precedes the current position
(?<=\$)\d+ matches '50' in '$50'
(?<!abc)
Negative lookbehind — asserts 'abc' does NOT precede
(?<!\$)\d+ matches '50' in '€50'
^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$
Basic email address validation pattern
https?://[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=%]*
Match HTTP or HTTPS URLs
https://example.com/path ✓
^\d{1,3}(\.\d{1,3}){3}$
Match an IPv4 address pattern
192.168.1.1 ✓
^\+?[\d\s\-().]{7,20}$
Match a phone number in various formats
+1 555-123-4567 ✓
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
Match a CSS hex color code
#ff6600 ✓