
close
close
In a recent article called, PowerShell Problem Solver: PowerShell String Parsing with Substrings, I showed you some ways to parse strings with PowerShell. For many PowerShell beginners, splitting strings works just fine. Eventually you’ll realize that you want more control, and this is where regular expressions come into play. I am not going to try and teach your regular expressions from scratch. There is an entire chapter in the second edition of PowerShell in Depth that covers regular expressions in PowerShell. You should also take a few minutes to look at the help topic about regular expressions.
A regular expression is a way of using a pattern to describe some piece of data. Granted, coming up with the pattern can be time consuming. Let’s see if our string challenge can help shed some light on the subject. If you recall, I am starting with a string, presumably from a log.
$s = "Mailbox:9WJKDFH-FS349-1DSDS-OIFODJFDO-7F21-FC1BF02EFE26 (O'Hicks, Jeffery(X.))"
The goal is to extract O’Hicks, Jeffery(X.) from the string. I’ve intentionally modified my name to throw in a different character because you might face something similar. The examples I am going to show you should also work for simpler strings as well. If you know for a fact what your data will look like, you might even be able to get by with simpler patterns. But enough chat. The simple way to even test if there is a matching pattern is with the –Match operator.
advertisment
$s -match "\S+,\s\S+"
The stuff to the left of the –match operator is the regular expression pattern. Here’s how it breaks down, and it is case-senstive:
The –match operator will return true or false. If true, you can look at the built-in $matches variable to see what matched.
Using the -match operator in Windows PowerShell. (Image Credit: Jeff Hicks)
$t = $matches.values
$t.Substring(1,$t.length-2)
Parsing a relevant part of the string in Windows PowerShell. (Image Credit: Jeff Hicks)
advertisment
$s -match "\b\S+,\s\S+\(\w{1}\.\)"
There are a few additions here.
As you can see, now I have exactly the result I need without any additional parsing.
Our modified command with the -match operator in Windows PowerShell. (Image Credit: Jeff Hicks)
advertisment
[regex]$rx="\(.*\)"
This is a variation on what I used before.
The REGEX object gives you a bit more control. Pipe $rx to Get-Member and you should see something like this:
Using the REGEX object in Windows PowerShell. (Image Credit: Jeff Hicks)
$rx.IsMatch($s)
You can also use the Match method:
$rx.Match($s)
Using the Match method in Windows PowerShell. (Image Credit: Jeff Hicks)
Grabbing the value with the Match method in Windows PowerShell. (Image Credit: Jeff Hicks)
[regex]$rx="\b\S+,\s\S+\(\w{1}\.\)"
$rx.Match($s).value
Or if you want to show off your head-spinning PowerShell skills, try this one-liner:
([regex]"\b\S+,\s\S+\(\w{1}\.\)").Match($s).Value
The advantage to a regular expression is that you can find matching patterns within strings without having to worry about how long the string is or how it might be formatted. You still need to know your data and it must be consistent. If some lines show the data I want as “O’Hicks, Jeffery(X.)” but it might also be “O’Hicks, Jeffery{X.}” or “O’Hicks, Jeffery X”, it might make your regular expression pattern a bit more complicated.
I know many IT pros are new to regular expressions and find them difficult, but like anything, it simply takes practice. So the next time you are looking to parse some string for some nugget of information, see if regular expressions can make your life easier. But I strongly believe that if you want to be taken seriously as a PowerShell professional, then you need to develop at least some basic proficiency with regular expressions. We’ll wrap up this mini-series next time with another aspect of regular expressions – named captures.
More in PowerShell
Microsoft’s New PowerShell Crescendo Tool Facilitates Native Command-Line Wraps
Mar 21, 2022 | Rabia Noureen
Most popular on petri