Wednesday, January 13, 2010

Regular Expressions with PowerShell

I came into Regular Expressions using Perl, and I must say I liked it and enjoyed it immensely once I was able to actually figure out how to do the syntax, never mind doing captures in order to use specific values from strings later. Although, like most things, once you do it then its simple and you think, "that was not so bad" when you look at something that looks like you kid typed when you left the computer on.

Perl was one thing, PowerShell is another. Now that I have this figured out its simpler, but finding the information on it was not so simple, although I will say one thing that made it easier was having a Designer tool that went over some of it. The Rad Software Designer is a very good tool for being able to help figure out the syntax as well as give you a place to assure that the results will be what you want, and since PowerShell is based off the .Net Regular Expression implementation the result will work fine there.

My goal was to be able to match a line like this:
Assertions: 8 right, 0 wrong, 0 ignored, 0 exceptions
So I could get the numerical values for display, turns out I only needed this:
:\s(?\d+)\sr.*(?\d+)\sw.*(?\d+)\si.*(?\d+)\se.*
...and then I could use some named capture values to then display the information I need. Now if I could only figure out how to turn off the True statement being displayed, that would be great.

No comments: