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 microsoft.public.windows.powershell, 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"

6 comments:

mama said...

i was waitin' for this type of article and i have gained some useful information from this site. thanks for sharing this information.

www.n8fan.net

Cindy Dy said...

Thank you for this post. Keep it up. Hope to read more post from you guys.

Viena
www.gofastek.com

Zachary Loeber said...

Not certain why but this little helper list resonates with me and helped step me through a few regexes I needed to create pretty quickly. Thanks for sharing.

Anonymous said...

thankyou. straight to the point info.

sarah lee said...

I really enjoyed reading your article. I found this as an informative and interesting post, so i think it is very useful and knowledgeable. I would like to thank you for the effort you have made in writing this article.


edupdf.org

Leslie Lim said...

Man I love this site. It is just so awesome. Whatever you want, all are available here.

www.imarksweb.org