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 multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff is a regular contributor to a variety on online sites, as well as frequent speaker at technology conferences and user groups. Keep up with Jeff and his projects at http://jdhitsolutions.com/blog.

comments powered by Disqus