PowerShell How-To
Matching and Using Regex Groups with PowerShell
Even though it has somewhat of a checkered reputation, being able to wield it will save you a ton of time.
- By Adam Bertram
- 09/30/2015
There's a saying that I've heard numerous times about regular expressions (regex). It goes something like this: "If you have a problem that requires regex to fix then you've got two problems." Needless to say, regex is known for being hard to work with. This might be the case if you need to match lots difficult strings but typically I've found that I don't need to get complicated. Even though regex has a checkered past, don't let this sway you from using it when you need to.
When you need to leverage regex in PowerShell you'll be using the .NET regex engine. Each language has its own subtleties so be aware of this if you find an example regular expression on the Internet somewhere and it doesn't work how you'd expect. The regex pattern might be similar which may be hard to spot but just one character makes a world of difference when using regex.
So what if you're writing a PowerShell script and you'd like to use regex to match strings? Two options that you have are:
- Use the [regex] type accelerator.
- Use the automatic variable $matches.
Let's go over each one and some examples on their usage. First, we'll cover the [regex] type accelerator. To use the [regex] type accelerator requires using [regex] and proceeding with double colons.
[regex]::
Once you type this, start hitting Tab. By doing so, you will expose all of the properties and methods that you can use the the [regex] type accelerator. For our purposes we're going to be focusing on the Match() method. The Match() method is a way to instruct PowerShell to attempt to match a string inside of another string. The Match() method has two parameters; the string you'd like to match on and the regular expression you'd like to test against.
Let's say you have a string abc123 and want to check to see if that string starts with an a. To do that, you can use the [regex] type accelerator. You can see that the string abc123 does start with an a because of the Success property is True.
Now what if you'd like to know what the string that was matched? To do this, we'll use regex groups. Whenever a match is found and a regex group is used; (), the [regex] type accelerator has a Captures property. This Captures property then has a property called Groups. This is a collection that contains lots of attributes of what was matched. The second element in that collection contains the actual value that was matched.
You'll see that I was able to see that the string bc123 was the value that was matched inside the group.
Now let's do the same task only this time using the automatic variable $matches. When using the -match and -notmatch operators, if a match is found, PowerShell will populate a variable called $matches automatically. $Matches is a collection that contains all of the match objects that were last found.
Coming from our last example, let's try to do the same thing using the $matches variable.
You'll see that this method is a little easier. I don't have to go through lots of different objects to find the desired value.
We just barely scraped the surface of what you can do with regular expressions in PowerShell. If you'd like more information on this topic I suggest visiting a blog post by John Cook. It goes into much more detail and covers a wide array of topics on using regular expressions in PowerShell.
About the Author
Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.