Metacharacters
Metacharacters are non-alphabetic characters that possess special meanings in regular expressions.
The set of metacharacters that can be used in extended regular expression syntax is as follows:
* + ? $ ^ . () | \ {} [
The following table describes all of these metacharacters except the square bracket [ metacharacter. You can use the [ metacharacter to construct bracket expressions.
Metacharacter | Description | Examples |
---|---|---|
* | Matches zero or more instances of the preceding atom. Matches as many instances as possible. | goo* matches my godness, my goodness, and my gooodness, but not my gdness. |
+ | Matches one or more instances of the preceding atom. Matches as many instances as possible. | goo+ matches my goodness and my gooodness, but not my godness. |
? | Matches zero or more instances of the preceding atom. | goo? matches my godness, my
goodness, and my gooodness, but not my gdness. colou?r matches color and colour. end-?user matches enduser and end-user. |
$ | Matches the end of the string. | end$ matches the end, but not the ending. |
^ | Matches the beginning of the string. The ^ metacharacter can also be used in bracket expressions. |
^severity matches severity level 5, but not The severity is 5. |
. | Matches any single character. | b.at matches baat, bBat, and b4at, but not bat or bB4at. |
() | Indicates that the characters within the parentheses should be treated as a character pattern. | A(boo)+Z matches AbooZ, AboobooZ,
and AbooboobooZ, but not AboZ or AboooZ. Jan(uary)? matches Jan and January. |
| | Matches one of the atoms on either side of the pipe character. | A(B|C)D matches ABD and ACD,
but not AD, ABCD, ABBD,
or ACCD. (AB | CD) matches AB and CD, but not ABD and ACD. |
\ | Indicates that the metacharacter following should be treated as a regular
character. The metacharacters listed in this section require a backslash escape
character as a prefix to switch off their special meaning. The \ metacharacter can also be used to construct backslash sequences. |
\* matches the * character. \\ matches the \ character. \. matches the . character. |
{m , n} | Matches from m to n instances of
the preceding atom, where m is the minimum and n is
the maximum. Matches as many instances as possible. Note: m and n are
unsigned decimal integers between 0 and 255.
|
f{1,2}ord matches ford and fford. N/{1,3}A matches N/A, N//A, and N///A, but not NA or N////A. |
{m ,} | Matches m or more instances of the preceding atom. | Z{2,} matches two or more repititions of Z. |
{m} | Matches exactly m instances of the preceding atom. | a{3} matches aaa. 1{2} matches 11. |