PowerShell Regular Expressions

A regular expression, or regex for short, is a sequence of characters that form a search pattern, to find in another piece of text, for example, to see if a piece of text matches a certain format or contains particular characters. These can be used, for example, in ‘if’ statements, to verify data. The example below looks for the word ‘Power’ in ‘Powershell’ and displays a message if it is found or not.

if ("Powershell" -match "Power")
{

    Write-Host "Match found."

}
else
{

    Write-Host "No match found."

}

In this example, ‘-match’ is used, which is a special type of operator, to find a match in the string on the left, using the pattern on the right. Note that, by default, a search is case-insensitive, so in the above example ‘Power’ and ‘power’ would produce a successful match. To specify a case-sensitive match, a ‘c’ needs to be placed in front of ‘match’ in the operator, ‘-cmatch’. In order to explicitly specify a case-insensitive match, an ‘i’ needs to be placed in front of ‘match’ in the operator, ‘-imatch’.

Regular Expression Characters

Regular expression characters are special characters that can be used in a search pattern, for example, to find specific characters at the beginning or end of a search pattern, within a particular range and much more. Below is a list of the regular expression characters available within PowerShell, together with some examples. All of the examples would produce a successful match.

Character Description Example
. Matches a single character. “Powershell” -match “P…r”
[value] Matches at least one character specified within the brackets. “Powershell” -match “P[aeiou]wer”
[range] Matches at least one character specified within a range. “Powershell” -match “Pow[a-f]r”
[^value] Matches any character except those within the brackets. “Powershell” -match “Po[^aeiou]er”
^ Matches characters located at the beginning of a string. “Powershell” -match “^Power”
$ Matches characters located at the end of a string. “Powershell” -match “shell$”
* Matches any instances of the preceding characters. “Powershell” -match “ower*”
? Matches zero or one occurrence of the preceding characters. “Powershell” -match “ower?”
\ Matches the character that follows as an escaped character. “Powershell/Python” -match “\/”

When specifying a range, it can either be numeric or alphabetical. An alphabetical range is case-sensitive so “A-Z” and “a-z” are different. It is also possible to specify more than one range within the square brackets, so “[A-Za-z0-9]” would search for all letters, both upper and lower case, as well as numbers, zero through nine.

Regular Expression Qualifiers

Regular expression qualifiers are used to help make the search pattern more specific, for example, a match must occur a specific number of times or within a specific range. Below is a list of qualifiers that are available within PowerShell.

Qualifier Description
* Must match zero or more times.
+ Must match one or more times.
? Must match no more than one time.
{n} Must match n times.
{n,} Must match at least n times.
{n,m} Must match at least n time, but not more than m times.

Regular Expression Shortcuts

To make creating regular expressions easier, a number of shortcuts are available, which can be found below.

Shortcut Description
\d Matches any decimal digit. Equivalent to [0-9].
\w Matches any word character. Equivalent to [0-9A-Za-z_].
\D Matches any non-digit.
\W Matches any non-word character such as space.
\S Matches any non-whitespace character.

Example Regular Expressions

Below are examples of regular expressions that utilise some of the characters, qualifiers and shortcuts above.

Example Description
^[A-Z]{2}[0-9]{6}[A-Z]{1}$ Check the format of a valid UK National Insurance Number. Note this only checks for a valid format of two letters, followed by six digits and then one more letter. It doesn’t enforce all the rules, for example, the first letter cannot be D, F, I, Q, U or Z.
^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$ Check for a valid IP address, one to three digits followed by a period, then another one to three digits and so on, e.g. “192.168.15.20”.
^\d{3}-\d{2}-\d{4}$ Check for a valid American Social Security Number, three digits, followed by a hyphen, two digits, followed by another hyphen and then four more digits.