How-to: Regular Expressions

Use -match , -notmatch or -replace to identify string patterns. More complex patterns can be matched by adding a regular expression.

RegEx characters: ^ . [ ] - g G ? + * p P w W s S d D $


Match exact characters anywhere in the original string:
PS C:> 'Ziggy stardust' -match 'iggy'
True

Notice that wildcards are implicit, so -match '2' will match against 2, but also against 25 or 402, and -match '' will match against everything. See below for precise matching by position.

Match multiple characters

Match any (at least one) of the characters - place the options in square brackets [ ]
PS C:> 'Ziggy stardust' -match 'Z[xyi]ggy'
True


Match a range (at least one) of characters in a contiguous range [n-m]
PS C:> 'Ziggy stardust' -match 'Zigg[x-z] Star'
True

Match anything but these, a caret (^) will match any character except those in brackets
PS C:> 'Ziggy stardust' -match 'Zigg[^abc] Star'
True

Match anything but these characters, specify in one or more contiguous ranges [^n-m]
PS C:> 'abc' -match '[^abc-ijk-xyz]'
False
PS C:> 'abc' -match '[^ijk-xyz]'
True

Match any one of the special characters which are Not Allowed in a SharePoint filename:
PS C:> 'Ziggy sta#rdust' -match '[~#%&*{}\\:<>?/|+"]'
True

The backslash in the expression above has to be escaped, doubled to \\
The only characters that needs to be escaped inside a character class are the backslash \ and the closing bracket ].

Match by Position

Match only if at the beginning of the line: ^
PS C:> 'no alarms and no surprises' -replace '^no',''
alarms and no surprises

Match only if at the end of the line: $
PS C:> 'There must be some way out of here said the joker to the joker' -replace 'joker$','thief'
There must be some way out of here said the joker to the thief

Match a range of numbers

Match a number if it is between 0 and 5,
PS C:> 4 -match "[0-5]"
True


This is matching any digit/character, so 25 also matches because the 2 is between 0 and 5, not really what is wanted:
PS C:> 26 -match "[0-5]"
True

Running this for all the numbers from 1 to 10:
PS C:> (1..10) -match "[0-5]"
1
2
3
4
5
10

In this case to 10 matched because the '1' in 10 is between 0-5

Anchoring to look for something that starts (^) and ends ($) with a number between 0 and 5.
PS C:> 26 -match "^[0-5]$"
False

PS C:> 3 -match "^[0-5]$"
True

To match larger numbers requires a more complex regex, to test the numbers 0 to 19 test a match with 0-9 OR (|) the number 1 followed by 0-9:

PS C:> 12 -match "^([0-9]|[1][0-9])$"
True

To test the numbers 0 to 99:

PS C:> 45 -match "^([0-9]|[0-9][0-9])$"
True

Wildcards

A period . will match a single character:

PS C:> 'cat' -match 'c.t'
True
PS C:> 'Ziggy stardust' -match 's..rdust'
True

Match zero or more instances of the preceding character: *

PS C:> 'Ziggy stardust' -match 'X*star'
True
PS C:> 'Ziggy stardust' -match 'X*jones'
False

Match zero or more instances of the preceding character, matching as much as possible: ?

PS C:> 'AaaBbb' -match 'X?C'
False
PS C:> 'AaaBbbCcc' -match 'X?C'
True

Match One or more instances of the preceding character, matching as much as possible: +

PS C:> 'AaaBbbCcc' -match 'A+C'
False
PS C:> 'AaaCcc' -match 'A+C'
True

Match special text

Match the character that follows as an escaped character by escaping with a backslash \

PS C:> 'Ziggy$' -match 'Ziggy\$'

This is different from the normal PowerShell escape character (the backward apostrophe), but it follows industry-standard regex syntax.

Match any character in a character class: \p{name}

Supported names are Unicode groups and block ranges for example, Ll (Letter, Uppercase), Nd (Number, Decimal Digit), Z (All separators), IsGreek, IsBoxDrawing.

PS C:> 'ZiGGY Stardust' -match '\p{Ll}+'

Match text not included in groups and block ranges: \P{name} .

PS C:> 1234 -match '\P{Ll}+'

Match any word character: \w

Meaning letters and numbers. This is roughly equivalent to [a-zA-Z_0-9] but will also match foreign letters with accents: (áåäæçèπΈψ etc) but not unicode symbols or punctuation:

PS C:> 'Ziggy' -match '\w+'
True
PS C:> '~~@ ;;' -match '\w+'
False

Match any non-word character \W

This is roughly equivalent to [^a-zA-Z_0-9] but foreign letters with accents will also be considered part of a word.

PS C:> 'Ziggy' -match '\W+'
False
PS C:> '~~@ ;;' -match '\W+'
True

Match any white-space: \s

This is equivalent to [ \f\n\r\t\v]

PS C:> 'Ziggy stardust' -match '\s+'
True

Match any non-white-space: \S

This is equivalent to [^ \f\n\r\t\v]

PS C:> 'Ziggy' -match '\S+'
True

Match any decimal digit: \d

This is equivalent to \p{Nd} for Unicode and [0-9] for non-Unicode.

PS C:> 'klmn0pq' -match '\d+'

Match any non-digit: \D

This is equivalent to \P{Nd} for Unicode and [^0-9] for non-Unicode.

PS C:> '789o123' -match '\D+'

Match by number of occurences

Exactly n matches: {n}

PS C:> 'sssss' -match '^s{5}$'
True
PS C:> 'sssss' -match '^s{4}$'
False

Match n or more matches, matching as much as possible: {n,}

PS C:> 'sssss' -match '^s{3,}$'
True

Match between n and m matches, matching as much as possible: {n,m}

PS C:> 'sssss' -match '^s{6,9}$'
False

Matching as little as possible can be done by appending a ?

*? Zero or more matches
+? One or more matches
?? Zero or one matches
{n}? Exactly n matches
{n,}? N or more matches
{n,m}? Between n and m matches

Replace using capture groups

Replace with the entire matched string: $&

PS C:> 'ABCD' -replace "[BC]",'$&x'
ABxCxD

Replace with a capture group: $1, $2, …

PS C:> 'ABCD' -replace "([AC])(.)",'$2-$1'
B-AD-C

To create a named capture group, put parentheses around it like normal, then add '?<groupname>' to the beginning of the capture. This stores the group under the name groupname.

Replace with a named capture group: ${name}

PS C:> 'ABCD' -replace "(?<foo>[AC])(?<bar>.)",'${bar}-${foo}'
B-AD-C

-match and -replace are case insensitive, as are -imatch and -ireplace.
For case sensitive matching, use -cmatch and -creplace

The -match operator will set the $matches variable whenever a match is found.
The -replace operator does not set the $matches variable.

In addition to all the above PowerShell also supports the quantifiers available in .NET regular expressions.

The .Net framework uses a traditional NFA regex engine, to learn more about regular expressions look for the book Mastering Regular Expressions by Jeffrey Friedl

“Mere enthusiasm is the all in all. . . .Passion and expression are beauty itself” ~ William Blake

Related PowerShell Cmdlets

Set-Content - Find and Replace text within files.
Comparison -like, -lt, -gt, -eq, -ne, -match.
Wildcards - Match multiple items.
Escape characters - double \\ to escape them.


 
Copyright © 1999-2024 SS64.com
Some rights reserved