Powershell to test your Regexes
Previously I've always used a tool like Roy Osherove's
The Regulator to play with and test my regular expressions simply to avoid writing a tiny program (even with
Visual Notepad).
As great as The Regulator and it's brethren are, I just want to play with my regexes as quickly as possible. More often than not I don't need the big fancy UI offered bu these tools which is why I'm digging
Powershell to play with my regexes at the moment.
I posted about using regex in Powershell in
Tonight Powershell saved my music library - it's so easy.
Just recently a colleague asked for some help with a regex so Powershell was fired up and, sure enough, we had banged out the appropriate (albeit simple) solution in no time.
First of all, create your regex
$re = [regex]"^[A-za-z0-9]*$"
Which should match any single alphanumeric word so let's put it to the test.
$re.Match("test")
Groups : {test}
Success : True
Captures : {test}
Index : 0
Length : 4
Value : test
All good so far (note the value of Success = True). In fact, you can even use the
-match operator:
$re -match "test test"
False
No match here which is what we want...
$re -match "test%"
False
Still good until...
$re -match ""
True
Ah, in this case we don't want to match an empty string so a quick change to our regex:
$re = [regex]"^[A-za-z0-9]+$"
Quick swap of a '*' (0-n matches) for '+' (1-n matches) and we're away.
Another really neat feature of powershell is that they shipped the manual with the product.
get-help about_regular_expression

PS Obviously, for the sake of brevity, I didn't include all my tests!
PPS I did try
get-help about_changing_oil_filter but they only included instructions for Powershell :(
PPPS Reason 9 is coming - I've been away for a week