Thursday, September 3, 2009

A Regular Expression Cheat Sheet for PowerShell

Okay, so there's really not much that is PowerShell-specific about this cheat sheet, but I wrote it up in response to a post on, and I thought I'd share it here, since I know a lot of new PowerShell users haven't been exposed to Regular Expressions very much.

This is not meant as an exhaustive reference on regular expressions, but just something that may be helpful if you get a little stuck. I hope you find it helpful.

1. What kind of character is it?

[] - any of the characters inside the brackets will match
(use the dash to indicate a range)
Examples: [a-z] will match any letter.
[aeiou] will match any vowel
\w - "word" characters. Basically matches [a-z0-9_]
\s - "whitespace" characters. matches spaces, tabs, etc.
\d - any digit. Basically matches [0-9]
\t - a tab character
. - any character

2. How many characters?
(the below appear after the character class)

{x} - matches x number of characters
{x, y} - matches minimum x number of characters, maximum y characters
Examples: \d{4} matches 4 digits
* - matches ZERO or more of the character (as many as possible)
+ - matches ONE or more of the character (as many as possible)
*? - matches ZERO or more characters (as few as possible)
+? - matches ONE or more characters (as few as possible)

3. Where is the character?

\b - matches a word boundary, without actually absorbing any characters
^ - matches the beginning of a string
$ - matches the end of a string

4. Grouping

() - any characters between the parentheses will be their own group
(try checking the value of $matches after using -match)
| - a pipe character is the OR character
Example: (one|two) will match the word "one" or the word "two"