Basic Characters

PatternDescriptionExample PatternMatched String
^Start of string (each line){regex} ^Hello”Hello World”
$End of string (each line){regex} World$”Hello World”
.Any character (except newline){regex} a.c”abc”, “adc”
|Or{regex} cat|dog”cat”, “dog”
\Escape special characters{regex} a\.b”a.b”

Quantifiers

PatternDescriptionExample PatternMatched String
*Zero or more occurrences{regex} ab*c”ac”, “abc”, “abbc”
+One or more occurrences{regex} ab+c”abc”, “abbc”
?Zero or one occurrence{regex} ab?c”ac”, “abc”
{n}Exactly n occurrences{regex} a{3}”aaa”
{n,}n or more occurrences{regex} a{2,}”aa”, “aaa”, “aaaa”
{,m}Up to m occurrences{regex} a{,3}”a”, “aa”, “aaa”
{n,m}Between n and m occurrences{regex} a{1,3}”a”, “aa”, “aaa”

Character Classes

PatternDescriptionExample PatternMatched String
\dDigit (0-9){regex} \d\d\d”123”
\DNot a digit{regex} \D\D”AB”
\wWord character (a-zA-Z0-9_){regex} \w+”HelloWorld”
\WNot a word character{regex} \W+”!@#$“
\sWhitespace (space, tab, newline){regex} \s+” \t\n”
\SNot whitespace{regex} \S+”NoSpace”
\bWord boundary{regex} \bcat\b”The cat sat”
\BNot a word boundary{regex} \Ban”banana”
\A(Absolute) start of the string{regex} \AHello”Hello World”
\Z(Absolute) end of the string (before newline){regex} World\Z”Hello World”

Sets

PatternDescriptionExample PatternMatched String
[abc]Any character in the set{regex} b[ae]d”bad”, “bed”
[^abc]Any character not in the set{regex} [^xyz]+”uvw”
[a-z]Range of characters{regex} [a-f]+”abcdef”

Groups

PatternDescriptionExample PatternMatched String
()Grouping and capturing{regex} (ab)+”abab”
(?#)Comment; ignored by the engine{regex} a(?#this is a comment)b”ab”
(?P<name>A)Named capturing group{regex} (?P<word>\w+) \g<word>”test test”
(?:A)Non-capturing group{regex} (?:ab)+c”ababc”
(?P=name)Backreference to a named group{regex} (?P<char>.)(?P=char)”aa”, “bb”

Assertions

PatternDescriptionExample PatternMatched String
A(?=B)Positive lookahead: A followed by B{regex} Hello(?= World)”Hello” in “Hello World”
A(?!B)Negative lookahead: A not followed by B{regex} Hello(?! World)”Hello” in “Hello There”
(?<=B)APositive lookbehind: A preceded by B{regex} (?<=Hello)World”World” in “Hello World”
(?<!B)ANegative lookbehind: A not preceded by B{regex} (?<!Goodbye)World”World” in “Hello World”

Flags (Modifiers)

FlagCommon NameDescription
iCase-InsensitiveMatches regardless of case (e.g., a matches A).
gGlobalFinds all matches, not just the first.
mMultiline^ and $ match the start/end of lines (after/before newlines).
sDotall. matches any character, including newline.
uUnicodeEnables full Unicode support for character classes (\w, \d, \s, etc.).