Using Regular Expression Metacharacters

Use the metacharacters described in the table below when performing searches or creating rules:

Metacharacter Description
^ Anchors match to the beginning of a line or string
$ Anchors match to the end of a line or string
abc Matches all of a, b, and c in order
fee|fie|foe Matches one of fee, fie, or foe
^(?!fee|fie|foe) Matches anything other than fee, fie, or foe
. Matches any character except newline
x? Matches 0 or 1 occurrences of x (x can be any character)
x* Matches 0 or more occurrences of x
x+ Matches 1 or more occurrences of x
x{m,n} Matches at least m occurrences of x but no more than n
[a-z0-9] Matches any single character of set
[^a-z0-9] Matches any single character not in set
\d Matches a digit, same as [0-9]
\D Matches a non-digit, same as [^0-9]
\w Matches an alphanumeric (word) character [a-zA-Z0-9_]
\W Matches a non-word character [^a-zA-Z0-9_]
\s Matches a whitespace character (space, tab, newline…)
\S Matches a non-whitespace character
\metachar Treat metacharacter as ordinary character; for example, \* matches the * character
(abc) Remembers the match for later backreferences
\1 Matches whatever first set of parens matched
\2 Matches whatever second set of parens matched
\3 and so on…
\b Matches a word boundary (outside [] only)
\B Matches a non-word boundary

Tip: Use the ^ and $ metacharacters around values that may be part of other values. For example, a value of 5 without anchors will also match with 15, 250, etc. To prevent it from matching with other values, place anchors around it: ^5$.

Leave a Reply