Metacharacter | Behavior | Example |
|---|
\b | Matches a word boundary, that is, the position between a word and a space. | er\b matches the "er" in "never" but not the "er" in "verb". |
\B | Matches a word non-boundary. | er\B matches the "er" in "verb" but not the "er" in "never". |
\d | Matches a digit character. Equivalent to [0-9]. | In the searched string "12 345", \d{2} matches "12" and "34". \d matches "1", 2", "3", "4", and "5". |
\D | Matches a nondigit character. Equivalent to [^0-9]. | \D+ matches "abc" and " def" in "abc123 def". |
\w | Matches any of the following characters: A-Z, a-z, 0-9, and underscore. Equivalent to [A-Za-z0-9_]. | In the searched string "The quick brown fox…", \w+ matches "The", "quick", "brown", and "fox". |
\W | Matches any character except A-Z, a-z, 0-9, and underscore. Equivalent to [^A-Za-z0-9_]. | In the searched string "The quick brown fox…", \W+ matches "…" and all of the spaces. |
[xyz] | A character set. Matches any one of the specified characters. | [abc] matches the "a" in "plain". |
[^xyz] | A negative character set. Matches any character that is not specified . | [^abc] matches the "p" in "plain". |
[a-z] | A range of characters. Matches any character in the specified range. | [a-z] matches any lowercase alphabetical character in the range "a" through "z". |
[^a-z] | A negative range of characters. Matches any character not in the specified range. | [^a-z] matches any character not in the range "a" through "z". |
{n} | Matches exactly n times. n is a nonnegative integer. | o{2} does not match the "o" in "Bob", but does match the two "o"s in "food". |
{n,} | Matches at least n times. n is a nonnegative integer. * is equivalent to {0,}. + is equivalent to {1,}. | o{2,} does not match the "o" in "Bob" but does match all the "o"s in "foooood". |
{n,m} | Matches at least n and at most m times. n and m are nonnegative integers, where n <= m. There cannot be a space between the comma and the numbers. ? is equivalent to {0,1}. | In the searched string"1234567", \d{1,3} matches "123", "456", and "7". |
(pattern) | Matches pattern and saves the match. You can retrieve the saved match from the SubMatches collection in Visual Basic Scripting Edition (VBScript) or from array elements returned by the exec Method in JScript. To match parentheses characters ( ), use "\(" or "\)". | (Chapter|Section) [1-9] matches "Chapter 5", and "Chapter" is saved for later use. |
(?:pattern) | Matches pattern but does not save the match, that is, the match is not stored for possible later use. This is useful for combining parts of a pattern with the "or" character (|). | industr(?:y|ies) is equivalent to industry|industries. |
(?=pattern) | Positive lookahead. After a match is found, the search for the next match starts before the matched text. The match is not saved for later use. | ^(?=.*\d).{4,8}$ applies a restriction that a password must be 4 to 8 characters long, and must contain at least one digit. Within the pattern, .*\d finds any number of characters followed by a digit. For the searched string "abc3qr", this matches "abc3". Starting before instead of after that match, .{4,8} matches a 4-8 character string. This matches "abc3qr". The ^ and $ specify the positions at the start and end of the searched string. This is to prevent a match if the searched string contains any characters outside of the matched characters. |
(?!pattern) | Negative lookahead. Matches a search string that does not match pattern. After a match is found, the search for the next match starts before the matched text. The match is not saved for later use. | \b(?!th)\w+\b matches words that do not start with "th". Within the pattern, \b matches a word boundary. For the searched string " quick ", this matches the first space. (?!th) matches a string that is not "th". This matches "qu". Starting before that match, \w+ matches a word. This matches "quick". |
\cx | Matches the control character indicated by x. The value of x must be in the range of A-Z or a-z. If it is not, c is assumed to be a literal "c" character. | \cM matches a CTRL+M or carriage return character. |
\xn | Matches n, where n is a hexadecimal escape value. Hexadecimal escape values must be exactly two digits long. Allows ASCII codes to be used in regular expressions. | \x41 matches "A". \x041 is equivalent to "\x04" followed by "1", (because n must be exactly 2 digits). |
\num | Matches num, where num is a positive integer. This is a reference to saved matches. | (.)\1 matches two consecutive identical characters. |
\n | Identifies either an octal escape value or a backreference. If \n is preceded by at least n captured subexpressions, n is a backreference. Otherwise, n is an octal escape value if n is an octal digit (0-7). | (\d)\1 matches two consecutive identical digits. |
\nm | Identifies either an octal escape value or a backreference. If \nm is preceded by at least nm captured subexpressions, nm is a backreference. If \nm is preceded by at least n captured subexpressions, n is a backreference followed by literal m. If neither of those conditions exist, \nm matches octal escape value nm when n and m are octal digits (0-7). | \11 matches a tab character. |
\nml | Matches octal escape value nml when n is an octal digit (0-3) and m and l are octal digits (0-7). | \011 matches a tab character. |
\un | Matches n, where n is a Unicode character expressed as four hexadecimal digits. | \u00A9 matches the copyright symbol (©). |