Prof. Powershell
Regular Expressions, The Basics
Part one of three on the basics of working with regular expressions.
- By Jeffery Hicks
- 08/14/2012
Last time we looked at using the -Match operator to compare strings. This operator is a regular expression operator, although the pattern can be a simple string:
PS C:\> get-acl c:\work | select -expand access | where {$_.identityreference -match "admin"}
A regular expression is a way of describing a piece of data by what it looks like. In this case, I'm looking for any IdentityReference that has “admin” anywhere in the value. But there's much more to this.
Regular expressions are a huge topic and can seem overwhelming. I know I felt that way when I first started working with them. The good news is that I can help you with some basics. PowerShell supports the full regular expression library from the .NET Framework and you'll come across some very gnarly examples, but don't let them scare you off.
Remember, a regular expression is just a pattern that describes a piece of data. PowerShell compares your pattern to the data and if it matches returns True. Here's an easy one. What type of object does this look like?
PS C:\> $t='powershell rules'
I'm assuming you would say it is a string. Regular expressions use a concept of character classes which simplify the process. You may beg to differ at first, but with a little experience you'll understand. For string characters the class is \w. Normally you don't need to pay attention to case, but you do here. This is a lower case w preceded by a slash. All the classes you will be using will be proceeded by a slash.
What does this look like in PowerShell?
PS C:\> $t -match "\w"
True
But what did it match on? Whenever you use -Match, you will automatically get a special $Matches object:
PS C:\> $matches
Name Value
---- -----
0 p
PowerShell matched the regular expression pattern on the letter ‘p' and then stopped. A character class can be modified (or quantified) for a more granular match. The modifiers you are likely to use are:
- * match any number multiple instances of the preceding character or character class from 0 to infinity
- + match one or more instances of the preceding character or character class
- ? match a single instance of the preceding character or character class
- ^ match the beginning character or character class
- $ match the end character or character class
So we might try this:
PS C:\> $t -match "\w+"
True
PS C:\> $matches
Name Value
---- -----
0 powershell
Now we matched on the complete word. The match ended when we hit something that didn't match, in this case the space. The \w class matches letters and numbers:
PS C:\> $t="Power123Shell Rules"
PS C:\> $t -match "\w+"
True
PS C:\> $matches
Name Value
---- -----
0 Power123Shell
If you want to match numbers, use the \d character class:
PS C:\> $t -match "\d"
True
PS C:\> $matches
Name Value
---- -----
0 1
Again, if we want to match all the numbers we need to modify the pattern:
PS C:\> $t -match "\d+"
True
PS C:\> $matches
Name Value
---- -----
0 123
The last bit I want to show is how to quantify the number of matches. You do this using {min,max}:
PS C:\> "1234abc" -match "\d{1}"
True
PS C:\> $matches
Name Value
---- -----
0 1
This pattern says find a single number:
PS C:\> "1234abc" -match "\d{1,3}"
True
PS C:\> $matches
Name Value
---- -----
0 123
Now I said match on a number that is at least one digit and no more than three. We can also combine classes. I'll leave you with a semi-practical example:
$pass=Read-Host "Enter a new password that starts with a 3 digit number followed by at least 3 but no more than 7 alpha characters."
Now I can use a regular expression pattern to validate:
$pass -match "^\d{3}\w{3,7}$"
The pattern is using the anchor characters I showed you last time. If this matches then I can carry on and use the variable.
That's more than enough for this week. We'll step it up in the next lesson, next time.
About the Author
Jeffery Hicks is an IT veteran with over 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 is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.