Metacharacters


The real power of regular expressions is in the use of metacharacters. Metacharacters allow you to construct expressions that match specific text patterns in an extremely flexible way.

Metacharacters and their meanings are listed below.

Metacharacter
Meaning
. (period)
Matches any one character no matter what the character is
?
Matches the character immediately before it either zero times or one time
*
Matches the character immediately before it any number of times including zero (the character may not be in the string at all)
+
Matches the character immediately before it one or more times (the character must be in the string at least once)
^
Indicates that the characters which follow are at the start of the string only
$
Indicates that the characters which precede it are at the end of the string
\d
Matches any decimal digit
\D
Matches any character that is not a decimal digit
\s
Matches a tab or space character
\S
Matches any character that is not a tab or a space
\w
Matches any letter, any digit, or the underscore character
\W
Matches any character which is not a letter, a digit, or the underscore
\
Escape character allowing the use of any of the metacharacters with their regular keyboard meaning. For instance, \. matches a period (.) in a regular expression. A period (.) matches any one character no matter what the character is.

Here are some examples which illustrate the use of metacharacters.

Regular Expression
Matches
Reason
up.own
uptown
updown
up own
Period (.) matches any character, even a space.
.exe
aexe
123exe
1exejfg
Period (.) matches any character, even a space.
\.exe
.exe
Backslash (\) tells NetTracker you are looking for a period, not using the period as a metacharacter.
ab?cdef
acdef
abcdef
abcdefg
acdefghij
Question mark (?) matches both zero instances of b and one instance of b.
ntcgi\?johnswift
ntcgi?johnswift
Backslash (\) tells NetTracker you are looking for a question mark, not using the question mark as a metacharacter.
abc*defg
abdefg
abcdefg
abcdefghij
abcccccdefg
Asterisk (*) matches the c zero or more times.
abc+defg
abcdefg
abccdefg
abcdefghij
abcccdefghij
Plus (+) matches the c one or more times.
free
free
freedom
innisfree
By default, text is matched wherever it is found in the text string.
^free
free
freedom
Caret (^) only matches the beginning of the text string.
free$
free
innisfree
Dollar sign ($) only matches at the end of the text string.

In addition to metacharacters, you can use four other characters to indicate the relationships between various parts of the regular expression.

Character
Meaning
|
Tells NetTracker to match the text to the left of the pipe or the text to the right of the pipe
[ ]
Contains a set of characters and tells NetTracker to match any character within that set
( )
Indicates that the part of the expression that is within the parentheses is to be considered as one unit
^ (first character inside brackets)
Negates the set of characters in brackets so that text must contain a character at that point in the expression but it cannot be any of the characters inside the brackets

Here are some examples which illustrate the use of these characters.

Examples
Matches
Does Not Match
Reason
they (would|should)
they would
they should
they should have
they would have
 
Parentheses and pipe indicate text must contain at least one instance of either would or should
[cz]one
cone
zone
one
Brackets indicate either a c or a z must be present for a match
[^abc]xyz
dxyz
mxyz
axyz
bxyz
cxyz
xyz
Brackets indicate there must be a character at that point in the expression, but the caret indicates that character cannot be a, b, or c