Shay pointed me to this excellent series about Regular Expressions Regular Expression Webcast Series , While following this series to fresh up my Regex skill’s, I will post about PowerShell Specifics in this series here.
There are more ways to work with regular expressions with PowerShell, for following the exercises in Part 2 of the series, we can limit us to the –Match Operator.
I this Post I show how to do the presented exercises in PowerShell (Note follow the Second webcast to see complete examples and background I just picked some examples to show the translation to using the –Match operator in PowerShell.
Regular expressions can be used in a lot of places in Powerhell so adding regex skills to your PowerShell toolset will be very profitable in the long time !
So let’s get started with watching the Webcasts and following allong in PowerShell, picking up PowerShell regex specifics on the way ..
First a little caveat you might encounter with the –Match Operator and might confuse you, if you do not know about this behavior, the –Match operator will react different on scalar values and Collections :
PoSH> '7' -match '7'
True
PoSH> '7','77' -match '7'
7
77
PoSH> '7' -match '7'
True
PoSH> $matches
Name Value
---- -----
0 7
In the case of a Scalar a Boolean value is return if matched and in case of a collection the matched items are returned.
Note that you can check up on the actual Matchs(and or submatches but more about that later) by inspecting the $matches variable
another thing you have to take note of is that the –Match operator is Case Insensitive by default, as we can see in the Word examples later in the second webcast.
PoSH> $names = 'jack','jim','betty','sue','barry','Henry','harry'
PoSH>> $names -match '^h'
Henry
harry
PoSH> $names -cmatch '^h'
harry
you can see to do a Case sensitive comparison, we can use the –cMatch Operator in PowerShell.
This is enough I think to get you trough the samples in the Webcast first the first 2 chapters, looking at the code samples below.
In the Second part so examples are show to mach ZIP codes here are some of of those examples translated to PowerShell to make it more easy to follow the examples in the webcast on PowerShell
'75603','78654','80765','90120','08765','76543','23456','56732' -match '7'
To make it more easy to follow the exercises we store this list in a variable, to make the line a bit shorter (although we can get the list back using the Up arrow, this makes it more readable) :
$zip = '75603','78654','80765','90120','08765','76543','23456','56732'
$zip -match '^(75|78)'
Some more examples :
$zip -match '^[275]'
$zip -match '^[3-6]'
$zip -match '^[^78]'
$names = 'jack','jim','betty','sue','barry','Henry','harry'
$names -match '^h'
$names -cmatch '^h'
$zip -match '^[0-9]{5}'
$zip -match '^[0-9]{2,5}'
$zip -match '^\d{5,}'
$zip -match 'a*'
I only watched the first to parts of the series but will follow up on how to do more advanced regex stuff in PowerShell when needed in following the Webcast series in PowerShell.
Enjoy,
Greetings /\/\o\/\/