Metacharacters | Meaning | Examples |
\ | Indicates that the match character is a special character, a literal, or a backreference. (A backreference repeats the previous match.) | \n matches the newline character,\\ matches \, \( matches (, \) matches ) |
^ | Matches the position at the start of the string. | ^A matches A if A is the first character in the string. |
$ | Matches the position at the end of the string. | $B matches B if B is the last character in the string. |
* | Matches the preceding character zero or more times. | ba*rk matches brk, bark, baark, and so on. |
+ | Matches the preceding character one or more times. | ba+rk matches bark, baark, and so on, but not brk. |
? | Matches the preceding character zero or one time. | ba?rk matches brk and bark only. |
{n} | Matches a character exactly n times, where n is an integer. | hob{2}it matches hobbit. |
{n,m} | Matches a character at least n times and at most m times, where n and m are both integers. | hob{2,3}it matches hobbit and hobbbit only. |
. | Matches any single character except null. | hob.it matches hobait, hobbit, and so on. |
(pattern) | A subexpression that matches the specified pattern. You use subexpressions to build up complex regular expressions. You can access the individual matches, known as captures, from this type of subexpression. | anatom(y|ies) matches anatomy and anatomies. |
x|y | Matches x or y, where x and y are one or more characters. | war|peace matches war or peace. |
[abc] | Matches any of the enclosed characters. | [ab]bc matches abc and bbc. |
[a-z] | Matches any character in the specified range. | [a-c]bc matches abc, bbc, and cbc. |
[: :] | Specifies a character class and matches any character in that class. | [:alphanum:] matches alphanumeric characters 0-9, A-Z, and a-z.[:alpha:] matches alphabetic characters A-Z and a-z.[:blank:] matches space or tab.[:digit:] matches digits 0-9.[:graph:] matches non-blank characters.[:lower:] matches lowercase alphabetic characters a-z.[:print:] is similar to [:graph:] except [:print:] includes the space character.[:punct:] matches punctuation characters .,"', and so on.[:space:] matches all whitespace characters.[:upper:] matches all uppercase alphabetic characters A-Z.[:xdigit:] matches characters permissible in a hexadecimal number 0-9, A-F, and a-f. |
[..] | Matches one collation element, like a multicharacter element. | No example. |
[==] | Specifies equivalence classes. | No example. |
\n | This is a backreference to an earlier capture, where n is a positive integer. | (.)\1 matches two consecutive identical characters. The (.) captures any single character except null, and the \1 repeats the capture, matching the same character again, therefore matching two consecutive identical characters. |