Regex Memo

Regex cheat sheet/reference.

Runs locally
Character Classes
.

Any character

Matches any single character except newline.

Example: a.b
Matches "axb", "a2b", "a*b"
Character Classes
\d

Digit

Matches any digit character (equivalent to [0-9]).

Example: \d+
Matches "123", "0", "999"
Character Classes
\D

Non-digit

Matches any non-digit character (equivalent to [^0-9]).

Example: \D+
Matches "abc", "hello", "-*"
Character Classes
\w

Word character

Matches alphanumeric characters and underscore (equivalent to [a-zA-Z0-9_]).

Example: \w+
Matches "user_name", "helper1"
Character Classes
\W

Non-word character

Matches any character that is not a word character.

Example: \W+
Matches "!", " @ ", " - "
Character Classes
\s

Whitespace

Matches whitespace characters including space, tab, newline.

Example: a\sb
Matches "a b", "a\tb"
Character Classes
\S

Non-whitespace

Matches any character that is not a whitespace.

Example: \S+
Matches "hello", "123", "!"
Character Classes
[abc]

Character set

Matches any single character listed inside the brackets.

Example: [aeiou]
Matches "a", "e", "i" (vowels)
Character Classes
[^abc]

Negated character set

Matches any single character not listed inside the brackets.

Example: [^0-9]
Matches "a", "x", "-", " "
Character Classes
[a-z]

Range

Matches any character in the specified range.

Example: [a-z]
Matches "a", "k", "z"
Anchors
^

Beginning of line

Matches the beginning of the string or line.

Example: ^hello
Matches "hello world", but not "say hello"
Anchors
$

End of line

Matches the end of the string or line.

Example: world$
Matches "hello world", but not "world map"
Anchors
\b

Word boundary

Matches a word boundary position (between a word character and a non-word character).

Example: \bcat\b
Matches "cat", but not "concat" or "cats"
Anchors
\B

Non-word boundary

Matches any position that is not a word boundary.

Example: \Bcat
Matches "concat", "copycat", but not "cat"
Quantifiers
*

Zero or more

Matches the preceding element zero or more times.

Example: ab*
Matches "a", "ab", "abbb"
Quantifiers
+

One or more

Matches the preceding element one or more times.

Example: ab+
Matches "ab", "abbb", but not "a"
Quantifiers
?

Zero or one

Matches the preceding element zero or one time.

Example: ab?
Matches "a", "ab"
Quantifiers
{n}

Exactly n times

Matches the preceding element exactly n times.

Example: a{3}
Matches "aaa"
Quantifiers
{n,}

At least n times

Matches the preceding element at least n times.

Example: a{2,}
Matches "aa", "aaa", "aaaa"
Quantifiers
{n,m}

Between n and m times

Matches the preceding element between n and m times.

Example: a{2,4}
Matches "aa", "aaa", "aaaa"
Groups & References
(abc)

Capturing group

Groups multiple tokens together and creates a capture group for extracting substrings.

Example: (abc)+
Matches "abc", "abcabc"
Groups & References
(?:abc)

Non-capturing group

Groups multiple tokens together without creating a capture group.

Example: (?:abc)+
Matches "abc", "abcabc"
Groups & References
a|b

Alternation

Matches either the element before or the element after the pipe.

Example: cat|dog
Matches "cat", "dog"
Lookarounds
(?=abc)

Positive lookahead

Asserts that the preceding element is followed by the lookahead group.

Example: a(?=b)
Matches "a" in "ab", but not "a" in "ac"
Lookarounds
(?!abc)

Negative lookahead

Asserts that the preceding element is not followed by the lookahead group.

Example: a(?!b)
Matches "a" in "ac", but not "a" in "ab"
Lookarounds
(?<=abc)

Positive lookbehind

Asserts that the following element is preceded by the lookbehind group.

Example: (?<=a)b
Matches "b" in "ab", but not "b" in "cb"
Lookarounds
(?<!abc)

Negative lookbehind

Asserts that the following element is not preceded by the lookbehind group.

Example: (?<!a)b
Matches "b" in "cb", but not "b" in "ab"
Flags
g

Global search

Retain the index of last match, allowing iterative matches across the input.

Example: /a/g
Finds all "a" occurrences in "banana"
Flags
i

Case-insensitive search

Ignore case differences when matching letters.

Example: /hello/i
Matches "Hello", "HELLO", "heLLo"
Flags
m

Multiline search

Changes behavior of ^ and $ to match start/end of lines, not just strings.

Example: /^a/m
Matches "a" at start of newlines inside a block