PowerShell regular expressions: Making string evaluation easier

I have been working on a couple of PowerShell projects recently that have involved some complex string manipulations. In doing so, one of the things that I have found that has made string evaluation easier is the use of regular expressions. Regular expressions are not unique to PowerShell. Regular expressions are simply character combinations that define a search pattern. For example, a regular expression can be used to check to see if a string contains one of a given list of characters.

In case you are wondering, regular expressions are supported by a variety of programming languages. They tend to be especially popular in Python but work just as well in PowerShell. So, with that said, let’s take a look at some simple examples of how you might evaluate a string in PowerShell based on a regular expression.

Before I get into showing you how to evaluate regular expressions, let’s take a look at a really basic string evaluation. I’m going to start out by creating a variable called $A and assigning my first name (Brien) as a value. From there, I can use an if statement to evaluate the variable’s contents. In the interest of keeping things simple, I will create a simple if then else statement that displays the word True if the value being evaluated matches the contents of the $A variable and displays the word False if not. Here is what the commands look like:

$A = “Brien”
If ($A = “Brien” {Write-Host ‘True’} else {Write-Host ‘False’}

If you look at the screenshot below, you can see that I have compared both my first and my last name to the contents of the $A variable, yielding results of True and False, respectively.

PowerShell regular expressions
The evaluations that you see in the screen capture above are examples of normal string comparisons. In the screen capture, I am comparing two strings to see if they are equal. That is why I am using the Equal (-eq) operator.

PowerShell regular expressions: Checking for a pattern

The first thing that you need to understand about evaluating PowerShell regular expressions is that it does not involve comparing strings to see if they are equal, but rather whether or not a pattern match exists. As such, the Equal operator (-eq) must be replaced by the Match operator (-match). Incidentally, you can sometimes use the equals sign (= but not -eq) in place of -match.

If you look at the next screen capture, the match operator would initially seem to do exactly the same thing as the equal operator. However, the match operator gives us the ability to perform complex pattern evaluations that we cannot easily do using the equal operator.

PowerShell regular expressions
So let me give you a really simple example of how pattern matching works. As you will recall, the $A string currently contains my first name. You might have noticed that my name has a somewhat unusual spelling. My name is spelled with an e instead of the a that most people use. We could use pattern matching to find out if my name is spelled with an e or with an a. Here is an example of a command that will check to see if an e appears in the string.

If ($A -Match “e”) {Write-Host ‘True’} else {Write-Host ‘False’}

In the screenshot below, you can see what happens when I check for the existence of both an e and an a.

PowerShell regular expressions
Granted, this is a really unsophisticated example. I am only checking to see if the letters e and a appear somewhere in the string. The command does not care what position they are in. However, it is relatively easy to limit the check to a specific position within the string.

Let’s pretend for a moment that I knew that whoever set up the $A variable would either spell my name as Brien or as Brian, and that I wanted to check the fourth character of the string to see if it was an e or an a. I could do so by using commands like these:

If ($A = “Bri[e]n” {Write-Host ‘True’} else {Write-Host ‘False’}
If ($A = “Bri[a]n” {Write-Host ‘True’} else {Write-Host ‘False’}

I’m sure you are probably wondering what the benefit is to evaluating a string in this way, rather than simply looking at a literal value as I did in the beginning. In all honesty, the example that I just showed you is impractical. But what if I was OK with either spelling of my name? In that case, we could use this exact technique and enclose multiple characters in brackets. Here is what the command would look like:

If ($A = “Bri[ea]n” {Write-Host ‘True’} else {Write-Host ‘False’}

So to get a pattern match, the first three characters would have to be Bri, the fourth character could either be an e or an a, and the last character would have to be an n. You can see how this works in the figure below.

Regular expressions can also be used to evaluate numeric patterns. Suppose, for instance, that a PowerShell script contained a variable named $A and that variable needed to store a three-digit number. You could use a regular expression match to make sure that the variable fits the criteria. Here is how it works:

$A = 123
If ($A -Match ‘[0-9][0-9][0-9]’) {Write-Host ‘True’} else {Write-Host ‘False’}

Notice that in this example, we can actually define the numeric range for each position. If we knew that the first character was always going to be six or higher, for example, we could replace [0-9] with [6-9]. The figure below shows a few examples.

Learning more

I have only begun to touch on the ways that you can evaluate strings using PowerShell regular expressions. Hopefully, however, you have begun to gain a sense of the flexibility that regular expressions give you. If you would like to learn more, Microsoft provides a full reference to the regular expression language.

Featured image: Shutterstock

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top