How to Match Strings Using Regex With PowerShell

Once you become familiar with regex, you’ll start to notice your code will become shorter, more efficient and concise. Although a little tough to understand at first I assure you it will be well worth your time to investigate using -match over -like in those more difficult matching situations.

Adam Bertram

April 19, 2024

4 Min Read
How to Match Strings Using Regex With PowerShell
Getty Images

Something I really like about PowerShell is how easy it is to match strings with other strings. I often find myself needing to see if a string is inside of another string. If I have a long string like ‘This is a long string’ and want to see if this string contains the word ‘long’ in it is as easy as using the -like operator and a couple asterisks.

Using -like you can also test if the string starts with another string.

Or you can see if the string ends with another string.

Using a comparison operator like -like operator is great for instances where you want to find if a string contains another string. But what if you’re faced with a situation where a simple set of letters and an asterisk or two just won’t cut it? you’re going to need to look into using regex.

A friend of mine always says “If you’re faced with a problem that needs regular expressions to solve, you’ve got two problems”. This stems from regular expressions’ (regex) long history of being an extremely hard to decipher language in of itself. Regex has long been known to be hard to understand. The way in which regex is parsed is a little confusing at first and I’m still no expert. I’ve just learned enough over the years to match the basic strings I’m looking for and know enough to search for the rest online.  I hope that you can too.

The most popular way to match strings using regex with PowerShell is by using the -match operator.  The -match operator is similar to -like in that it matches a string inside of a string. As with -like, I could just as easily match if the word ’long’ is inside the string ‘This is a long string’.

 

Notice this time though I didn’t have to include any asterisks. This is because match uses regex to make the match.  If I were to choose to use asterisks I’d be presented with some nasty red error text. The -like and -match operator are very different animals.

When faced with simple situations like this, it’s typically better to use -like instead of -match because regex adds a little overhead.

.02 milliseconds (5%) doesn’t seem like much but coming from personal experience once you create some super-complex strings and include a loop to match strings over and over that extra 5% of time really adds up. Where -match shines is through more complicated situations where -like just won’t work.

Let’s say you have a string ‘This is a long string’ and you want to see if the word ‘long’ starts at the 11th position in the string? There’s no way to do this with -like but with -match and regex it’s no problem.

Using the start of line character (^), the dot, which indicates any character and enclosing the number 10 inside of curly brackets tells PowerShell to start matching at the start of the line and match 10 of any character and then the characters l, o, n and g.

Using -match over -like also saves code too.  Perhaps you need to see if our string ‘This is a long string’ contains the word long or the word short? Using like we’d need to have two commands.

However, with -match I can trim that down considerably with the same effect.

 

Using the pipe symbol (|) meaning OR in regex I can match on the string if it contains the word long or short.

These are just two examples in a huge number of possibilities.

I realize if you’re new to regex this example may still leave you confused. It’s going to take a lot more than a single article to really make you understand regex. This is why I highly recommend getting familiar with the basics of regex. There are lots of tools out there to help you. A common regular expression tutorial site is regular-expressions.info.  This is a great site that has taught me a lot. Once you’re comfortable enough with coming up with examples, a site like Regexr is a must to test out your regular expressions. Sites like regexr allow you to test your regex to see if it’s going to match what you think it’s going to match.

Once you become familiar with regex, you’ll start to notice your code will become shorter, more efficient and concise. Although a little tough to understand at first I assure you it will be well worth your time to investigate using -match over -like in those more difficult matching situations.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like