Regex: List of Metacharacters
Positioning, Special characters, Lazy, Greedy, Lookahead, Lookbehind, etc.
Positional
Metacharacter | Explanation |
---|---|
^ | Beginning of line |
$ | End of line |
\A | Beginning of string |
\z | End of string |
\Z | End of string (excluding line endings) |
\b | Word boundaries |
\B | Non-word boundaries |
* The ^
and $
are used as "beginning and end of line" but this is the case in multiline mode (usually this is the default). In single-line mode, it is the beginning or end of the entire string.
Special characters
Metacharacter | Explanation |
---|---|
\ | Escape |
\t | Tab |
\n | Newline (CR) |
\r | Newline (LF) |
\f | New page |
\d | Digits |
\D | Non-digits |
\s | Whitespace (space, tab, neewline and new page) |
\S | Non-whitespace |
\w | Word (alphanumeric characters plus "_") |
\W | Non-word |
Wildcards and repetition (quantity specifiers)
Default | Lazy | Greedy | Explanation |
---|---|---|---|
. |
|
| Any one character (excluding line feed code) |
* | *? | *+ | 1 character/group immediately preceding 0 or more times |
+ | +? | ++ | 1 character/group immediately preceding 1 or more times |
? | ?? | ?+ | 1 character/group immediately preceding 0 or 1 times |
{n} | {n}? | {n}+ | 1 character/group immediately preceding n times |
{n,} | {n,}? | {n,}+ | 1 character/group immediately preceding n or more times |
{n,m} | {n,m}? | {n,m}+ | 1 character/group immediately preceding n or more times, but not more than m times. |
Default
Match the longest part that matches the condition. Preference is given to matching the entire pattern.
Lazy
Match the shortest part that matches the condition.
Greedy
Match the longest part that matches the condition. No preference is given to matching the entire pattern.
e.g. abcXdefX
Regex | Result | |
---|---|---|
Default | ^.*X | abcXdefX |
Lazy | ^.*?X | abcX |
Greedy | ^.*+X | Not match.* |
Greedy | ^.*+ | abcXdefX |
(*) "^.*+" matches all "abcXdefX", so the last `X` does not match.
Positional
Metacharacter | Explanation |
---|---|
() | Blocking |
| | Or (strings) |
[] | Or (one character) |
e.g. |
Regex | Explanation |
---|---|
abc|mno|xyz | Simple (abc, mno, or xyz) |
abc.*X|m+o|xy?z | Include metacharacters in candidates (abcXXX, mXo, xz, etc.) |
(abc|mno|xyz)X | Combine with other strings (abcX, mnoX, or xyzX) |
(abc|mno|xyz)* | Combine with quantity specifiers (abcabc, mnoabcxyz, etc.) |
e.g. []
Regex | Explanation |
---|---|
[abc] | Simple (a, b, or c) |
[^abc] | Negation (Other than a, b, c) |
[a-z] | Range (lowercase) |
[A-Z] | Range (uppercase) |
[0-9] | Range (digits) |
[a-zA-Z] | Or (a to z or A to Z) |
[a-z&&[d-f]] | And (a to z and d to f) |
[a-z&&[^d-f]] | Subtraction (a to z excluding d to f) |
Lookahead and Lookbehind
Metacharacter | Explanation | e.g. | Explanation |
---|---|---|---|
?= | Positive lookahead | foo(?=bar) | Matches "foo" followed by "bar". |
?! | Negative lookahead | foo(?!bar) | Matches "foo" not followed by "bar". |
?<= | Positive lookbehind | (?<=bar)foo | Matches "foo" preceded by "bar". |
?<! | Negative lookbehind | (?<!bar)foo | Matches "foo" not preceded by "bar". |