Regular Expressions

 

SHORT DESCRIPTION

Using regular expressions in Cmdlet parameters in Windows PowerShell

 

LONG DESCRIPTION

 

PowerShell supports a number of the regular expression characters:

 

Format

Logic  

Example

Value

Matches exact characters anywhere in the original value

"book" -match "oo"

.

Matches any single character

"copy" -match "c..y"

[value]

Matches at least one of thecharacters in the brackets

"big" -match "b[iou]g"

[range]

Matches at least one of the characters within the range. The Use of a hyphen (–) allows specification of contiguous characters.

"and" -match "[a-e]nd"

[^]

Matches any character except those in brackets

"and" -match "[^brt]nd"

^

Matches the beginning characters 

"book" -match "^bo"

$

Matches the end characters 

"book" -match "ok$"

*

Matches zero or more instances of the preceding character

"baggy" -match "g*"

?

Matches zero or more instances of the preceding character

"baggy" -match "g?"

\

Matches the character that follows as an escaped character

"Try$" -match "Try\$"

 

 

PowerShell supports the character classes available in .NET regular expressions:

 

 

Format  

Logic  

Example

\p{name}

Matches any character in the named character class specified by {name}. Supported names are Unicode groups and block ranges. For example, Ll, Nd, Z, IsGreek, IsBoxDrawing.

"abcd defg" -match "\p{Ll}+"

\P{name}

Matches text not included in1234 -match groups and block ranges specified in {name}.

"\P{Ll}+"

\w  

Matches any word character. Equivalent to the Unicode  character categories [\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}]. If ECMAScript-compliant behavior is specified with the ECMAScript option, \w is equivalent to [a-zA-Z_0-9].

"abcd defg" -match "\w+"

 

(this matches abcd)

\W  

Matches any nonword character. Equivalent to the Unicode   categories [^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}].

"abcd defg" -match "\W+"

 

(This matches the space)

\s  

Matches any white-space character.  Equivalent to the Unicode character categories [\f\n\r\t\v\x85\p{Z}].

"abcd defg" -match "\s+"

\S  

Matches any non-white-space character. Equivalent to the Unicode character categories [^\f\n\r\t\v\x85\p{Z}].

"abcd defg" -match "\S+"

\d  

Matches any decimal digit. Equivalent to \p{Nd} for Unicode and [0-9] for non-Unicode behavior.

12345 -match "\d+"

\D  

Matches any nondigit. Equivalent to \P{Nd} for Unicode and [^0-9] for non-Unicode behavior. 

"abcd" -match "\D+"

 

 

PowerShell supports the quantifiers available in .NET regular expressions. The following are some examples of quantifiers:

 

Format   

Logic  

Example

*

Specifies zero or more matches; for example, \w* or (abc)*. Equivalent to {0,}.

"abc" -match "\w*"

+

Matches repeating instances of   the preceding characters

"xyxyxy" -match "xy+"

?

Specifies zero or one matches; for example, \w? or (abc)?. Equivalent to {0,1}.

"abc" -match "\w?"

{n} 

Specifies exactly n matches; for example, (pizza){2}.

"abc" -match "\w{2}"

{n,}

Specifies at least n matches;  for example, (abc){2,}.  

"abc" -match "\w{2,}"

{n,m}

Specifies at least n, but no more than m, matches.

"abc" -match "\w{2,3}"

 

All of the comparisons shown in the above examples in the table above evaluate to true.

 

Note that the escape character for regular expressions is different than that of the PowerShell.  The character escape for regular expressions is the backslash "\".

 

SEE ALSO

For information about the match comparison operator, enter the following command at the PowerShell command prompt:

 

help about_comparison_operators

 

See the MSDN Documentation on Regular Expression Language Elements.