Using .NET, PowerShell has some powerful regex capabilities. In this article, I'm going go through, step-by-step on how to use use regex to capture groups with PowerShell using the [regex] type accelerator.

Unlike the Bash shell in Linux which only works with strings, with PowerShell, we work with objects which typically have properties already ready for us to use. We don't have to parse out near as many strings as you must do in the Linux world.

However, there still are times when you must be able to read a string and grab pieces of that string matching a specific pattern. This is where regular expressions (regex) come in.

There are a couple different ways to use regex and thusly regex groups with PowerShell.

  • The [regex] type accelerator
  • The automatic variable $matches

In this article, you'll be getting an introduction to the [regex] type accelerator to pull text from strings.

The [regex] type accelerator

One method that PowerShell can process regex is through the [regex] type accelerator. Without going into a lot of development speak, the [regex] type accelerator is a way to use a .NET class with a short expression.

To check out what you can do with this type accelerator bring up a PowerShell console and simply type [regex] followed by two double colons and begin hitting your tab key.

[regex]::

You'll notice that you'll be cycling through a lot of options. In this article, you'll only use the Match() method.

Let's say you have a string "My dog is named spot". Place this string into a variable called $aVariable.

$aVariable = 'My dog is named spot

In a script, you might not know what the value of this variable is so you'd like to use regex to pull out all of the characters to the left of the word "dog". This can be done using regex groups.

In a regex pattern, a group is signified by a pair of parentheses. Anything in these parentheses will be part of that particular group. Since I want to pull out all characters to the left of the word "dog" I will use the regex pattern .+ which is essentially equals a wildcard.

My regex pattern would look like this:

(.+)dog

Now that you have the pattern created, you can then use the Match() method to attempt the match on my variable.

PS> $aVariable = 'My dog is named spot'
PS> [regex]::Match($aVariable,'(.+)dog')
Regex output

Finding regex capture groups

You'll notice a few properties came out of that. The one we're interested in is called Groups. The Groups property will contain the characters from the string to the left of the word "dog". We'll need to dive a little deeper into this object to find those.

First, append .Groups to the end of the match reuslt to view only the Groups property. You'll see that this returns two objects.

PS> ([regex]::Match($aVariable,'(.+)dog')).Groups

Groups   : {0, 1}
Success  : True
Name     : 0
Captures : {0}
Index    : 0
Length   : 6
Value    : My dog

Success  : True
Name     : 1
Captures : {1}
Index    : 0
Length   : 3
Value    : My

The second object will always contain the text we're looking for. Knowing this, you will get more specific and pick this one from the two objects by referencing the index number of 1.

PS> [regex]::Match($aVariable,'(.+)dog').Groups[1]

Success  : True
Name     : 1
Captures : {1}
Index    : 0
Length   : 3
Value    : My

You're now just one step away! Now all you need to do is reference the Value property and you will get back the text you're looking for.

PS> [regex]::Match($aVariable,'(.+)dog').Groups[1].Value
My

Since there was a space right before the word "dog" I made sure that you captured that space by first assigning the output to a variable. Then, using the Length property, it checked out how many characters were inside of that string.

Summary

The [regex] type accelerator and regex capture groups in PowerSHell are a powerful way to match and parse strings. The next time you need to pull out a string inside of a string, give the [regex] type accelerator a try and add some parentheses around that regex to capture the values inside of the string.

If you'd like to learn more about some of the other features that PowerShell can do with regex, check out my other post Creating Custom Objects from Regex Matches.

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!