MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.

Prof. Powershell

Regular Expressions, Part 3

A last look at expressions, this time by trying to match on opposites.

Sometimes we want to match on opposites. For example, use a regular expression to find everything that is NOT a word character. We know what will happen with this:

PS C:\> 'Power$h3|| is fun' -match "\w+"
True
PS C:\> $matches

Name      Value
----      -----
0         Power

The opposite is the same pattern but a capital W. Remember, I told you these patterns can be case sensitive:

PS C:\> 'Power$h3|| is fun' -match "\W+"
True
PS C:\> $matches

Name      Value
----      -----
0         ||

Those spaces are also "matchable". The class \s will match any whitespace:

PS C:\> 'Power$h3|| is fun' -match "\s"
True

The actual match value isn't much help because it is a space. But you might use this pattern as a validator:

$p=Read-Host "Enter a password"
If ($p -match "\s") {
   Write-warning "Your password has a space which I don't like"
}

What do you think we use to match on all non-white spaces?

PS C:\> 'Power$h3|| is fun' -match "\S+"
True
PS C:\> $matches

Name      Value
----      -----
0         Power$h3||

The regular expression matches up until it doesn't,which in this case is the first space. But notice it grabbed numbers and the special characters. This is a handy class when you want to match everything BUT spaces or tabs, but it is very broad.

The last "opposite" is with digits. Normally use use \d to match a number. Use \D to match non-numbers.

PS C:\> 'Power$h3|| is fun' -match "\D+"
True
PS C:\> $matches

Name      Value
----      -----
0         Power$h

The regular expression matched until it hit a number.

So let's see everything we've covered thus far is some more practical examples. Open up a PowerShell prompt and try these commands. Before you run the command, predict what type of result you will get. You might be surprised where you can use regular expressions.

get-service | where {$_.name -match "\d{2}"}
get-service [a-b]*
get-process [cw]*
dir c:\windows | where {$_.PSIsContainer -AND $_.name -match "\d$"}
dir c:\windows | where {$_.name -match "\s"}
dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\kb[0-9]*

I can't guarantee you'll get something for every single command, especially the registry example. But I hope you take the time to try them out. Regular expressions are extremely valuable in PowerShell and like any language element, practice makes perfect.

If you haven't already done so, be sure to read the help topic About_Regular_Expressions.

About the Author

Jeffery Hicks is a Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)

comments powered by Disqus

Reader Comments:

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above