Basic Characters
| Pattern | Description | Example Pattern | Matched 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
| Pattern | Description | Example Pattern | Matched 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
| Pattern | Description | Example Pattern | Matched String |
|---|
\d | Digit (0-9) | {regex} \d\d\d | ”123” |
\D | Not a digit | {regex} \D\D | ”AB” |
\w | Word character (a-zA-Z0-9_) | {regex} \w+ | ”HelloWorld” |
\W | Not a word character | {regex} \W+ | ”!@#$“ |
\s | Whitespace (space, tab, newline) | {regex} \s+ | ” \t\n” |
\S | Not whitespace | {regex} \S+ | ”NoSpace” |
\b | Word boundary | {regex} \bcat\b | ”The cat sat” |
\B | Not 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
| Pattern | Description | Example Pattern | Matched 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
| Pattern | Description | Example Pattern | Matched 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
| Pattern | Description | Example Pattern | Matched 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)A | Positive lookbehind: A preceded by B | {regex} (?<=Hello)World | ”World” in “Hello World” |
(?<!B)A | Negative lookbehind: A not preceded by B | {regex} (?<!Goodbye)World | ”World” in “Hello World” |
Flags (Modifiers)
| Flag | Common Name | Description |
|---|
i | Case-Insensitive | Matches regardless of case (e.g., a matches A). |
g | Global | Finds all matches, not just the first. |
m | Multiline | ^ and $ match the start/end of lines (after/before newlines). |
s | Dotall | . matches any character, including newline. |
u | Unicode | Enables full Unicode support for character classes (\w, \d, \s, etc.). |