Positional
| Metacharacter | Description |
|---|
^ | 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 | Description |
|---|
\ | 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: 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.
| Default | Lazy | Greedy | Description |
|---|
. | | | Any one character (excluding line feed code) |
* | *? | *+ | 0 or more occurrences of the preceding item |
+ | +? | ++ | 1 or more occurrences of the preceding item |
? | ?? | ?+ | 0 or 1 occurrences of the preceding item |
{n} | {n}? | {n}+ | n occurrences of the preceding item |
{n,} | {n,}? | {n,}+ | n or more occurrences of the preceding item |
{n,m} | {n,m}? | {n,m}+ | n or more and not more than m occurrences of the preceding item |
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.
Select from candidates
| Metacharacter | Description |
|---|
() | Blocking |
| | Or (strings) |
[] | Or (one character) |
e.g. |
| Metacharacter | Description |
|---|
abc|mno|xyz | Simple abc, mno, or xyz |
abc.*X|m+o|xy?z | Include metachar 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. []
| Metacharacter | Description |
|---|
[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
| Meta | Description | e.g. | Description |
|---|
?= | 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”. |