Regex: List of Metacharacters

Positional

MetacharacterDescription
^Beginning of line
$End of line
\ABeginning of string
\zEnd of string
\ZEnd of string (excluding line endings)
\bWord boundaries
\BNon-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

MetacharacterDescription
\Escape
\tTab
\nNewline (CR)
\rNewline (LF)
\fNew page
\dDigits
\DNon-digits
\sWhitespace (space, tab, neewline and new page)
\SNon-whitespace
\wWord (alphanumeric characters plus ”_“)
\WNon-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.
DefaultLazyGreedyDescription
.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

RegexResult
Default^.*XabcXdefX
Lazy^.*?XabcX
Greedy^.*+XNot match.*
Greedy^.*+abcXdefX

(*) ^.*+ matches all “abcXdefX”, so the last X does not match.

Select from candidates

MetacharacterDescription
()Blocking
|Or (strings)
[]Or (one character)

e.g. |

MetacharacterDescription
abc|mno|xyzSimple
abc, mno, or xyz
abc.*X|m+o|xy?zInclude metachar in candidates
abcXXX, mXo, xz, etc.
(abc|mno|xyz)XCombine with other strings
abcX, mnoX, or xyzX
(abc|mno|xyz)*Combine with quantity specifiers
abcabc, mnoabcxyz, etc.

e.g. []

MetacharacterDescription
[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

MetaDescriptione.g.Description
?=Positive lookaheadfoo(?=bar)Matches “foo” followed by “bar”.
?!Negative lookaheadfoo(?!bar)Matches “foo” not followed by “bar”.
?<=Positive lookbehind(?<=bar)fooMatches “foo” preceded by “bar”.
?<!Negative lookbehind(?<!bar)fooMatches “foo” not preceded by “bar”.