Regular expressions play an important role in most text
parsing and text matching tasks. They form an important underpinning of the
-split and -match
operators, the switch statement, the
Select-String cmdlet, and
more. Tables B-1 through B-9 list commonly used regular
expressions.
Table B-1. Character classes: Patterns that represent sets of
characters
Character class
Matches
.
Any character except for a newline.
If the regular expression uses the SingleLine
option, it matches any character.
PS > "T" -match '.'
True
[characters]
Any character in the brackets. For
example: [aeiou].
PS > "Test" -match '[Tes]'
True
[^characters]
Any character not in the brackets.
For example: [^aeiou].
PS > "Test" -match '[^Tes]'
False
[start-end]
Any character between the
characters start and
end, inclusive. You may include multiple
character ranges between the brackets. For example,
[a-eh-j].
PS > "Test" -match '[e-t]'
True
[^start-end]
Any character not between any of
the character ranges start through
end, inclusive. You may include multiple
character ranges between the brackets. For example,
[^a-eh-j].
PS > "Test" -match '[^e-t]'
False
p{character
class}
Any character in the Unicode group
or block range specified by
{character
class}.
PS > "+" -match 'p{Sm}'
True
P{character
class}
Any character not in the Unicode
group or block range specified by
{character
class}.
PS > "+" -match 'P{Sm}'
False
w
Any word character. Note that this
is the Unicode definition of a word character,
which includes digits, as well as many math symbols and various
other symbols.
PS > "a" -match 'w'
True
W
Any nonword
character.
PS > "!" -match 'W'
True
s
Any whitespace
character.
PS > "`t" -match 's'
True
S
Any nonwhitespace
character.
PS > " `t" -match 'S'
False
d
Any decimal
digit.
PS > "5" -match 'd'
True
D
Any character that isn’t a decimal
digit.
PS > "!" -match 'D'
True
Table B-2. Quantifiers: Expressions that enforce quantity on the preceding
expression
Quantifier
Meaning
<none>
One match.
PS > "T" -match 'T'
True
*
Zero or more matches, matching as
much as possible.
Table B-8. Other constructs: Other expressions that modify a regular
expression
Construct
Description
(?imnsx-imnsx)
Applies or disables the given
option for the rest of this expression. Supported options
are:
i case-insensitive
m multiline
n explicit capture
s singleline
x ignore whitespace
PS > "Te`nst" -match '(?sx)T e.st'
True
(?#
)
Inline comment. This terminates at
the first closing parenthesis.
PS > "Test" -match '(?# Match 'Test')Test'
True
#[to
end of line]
Comment form allowed when the
regular expression has the IgnoreWhitespace
option enabled.
PS > "Test" -match '(?x)Test # Matches Test'
True
Table B-9. Character escapes: Character sequences that represent another
character
Escaped character
Match
<ordinary
characters>
Characters other than . $
^ { [ ( | ) * + ? match themselves.
a
A bell (alarm) u0007.
A backspace u0008 if in a []
character class. In a regular expression, denotes a word boundary (between
w and W characters) except within a
[] character class, where
refers to the backspace character. In a replacement pattern,
always denotes a
backspace.
A tab u0009.
A carriage return u000D.
v
A vertical tab u000B.
f
A form feed u000C.
A new line u000A.
e
An escape u001B.
ddd
An ASCII character as octal (up to
three digits). Numbers with no leading zero are treated as
backreferences if they have only one digit, or if they correspond to
a capturing group number.
xdd
An ASCII character using
hexadecimal representation (exactly two digits).
cC
An ASCII control character; for
example, cC is control-C.
udddd
A Unicode character using
hexadecimal representation (exactly four digits).
When followed by a character that
is not recognized as an escaped character, matches that character.
For example, * is the literal character
*.