PowerShell Pipeline

Regular Expressions I Use with PowerShell

From validating IP addresses to phone numbers, these are some of the handiest expressions in Boe's PowerShell toolbox. What are yours?

As this article states, I want to show some of the regular expressions that I use during my day-to-day activities.

But this isn't just about me and what I use them for. I would love to see what you in the PowerShell community do with regular expressions; share what you do in the comments below so others can benefit from everyone's amazing work!

IP Address Validation/Finding
If you recall, I previously wrote an article on using regular expressions to locate data and even offered up a regular expression to handle finding IP addresses. The RegEx expression that I found (because I didn't want to show off a simple one that could easily be defeated) is shown below:

^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$ 

Using it in a few PowerShell commands shows that it not only works, but also handles values that are out of range.

[Click on image for larger view.]

Splitting a String by Nth Character
If you have a string of characters and need to split it into groups of a particular count of characters, RegEx can make this very simple to do.

(?<>

In this example, I will split up a string of characters into groups of three. This is useful if you have to do some formatting of a MAC address from a single string of 16 characters so you can add colons or a dash. A few examples are below of where I will use the –Split operator instead of the usual –Match as I want to split the data up instead of trying to find something:

[Click on image for larger view.]

E-Mail Addresses
Whether it is validating an e-mail address on a tool that I wrote to create user accounts or searching a text document for e-mail addresses, a good regular expression to work with e-mail addresses is always in my toolkit.

As with validating an IP address, this can either be simple or complex depending on your requirements. Since an e-mail address in itself can be something simple or long and complex, I am going to show a simple RegEx that I use most of the time to locate addresses within a text file.

^[A-Z0-9_\-.]+@[A-Z0-9.-]+$ 

This simple expression will handle some of the possible characters and ensure that it follows a <username @ domain . com> notation.

[Click on image for larger view.]

North American Phone Numbers
Creating Active Directory accounts usually involves adding a phone number for a person's office. If you have a tool written to automate this process, something you might have need of is a way to validate that a phone number given is actually formatted properly or contains all of the necessary numbers to be a valid number.

My RegEx example below is something that I use as a validator in a tool to ensure that the proper number is given.

Note that this one is based on a particular need for myself so your mileage may vary with this example.

^\([0-9]{3}\)\s[0-9]{3}-[0-9]{4}$ 

As long as phone numbers used in my tool meet this requirement, they will be allowed.

[Click on image for larger view.]

Note that while the number was a valid phone number, it was not valid for the requirements that I have on my tool to create accounts that were given to me by management.

Those were some of my examples that I use during my time at work. This was only just a handful of the regular expressions, but I am more interested to see what you in the community have written and use. Please make sure to comment below with your examples so everyone can learn and use your examples!

About the Author

Boe Prox is a Microsoft MVP in Windows PowerShell and a Senior Windows System Administrator. He has worked in the IT field since 2003, and he supports a variety of different platforms. He is a contributing author in PowerShell Deep Dives with chapters about WSUS and TCP communication. He is a moderator on the Hey, Scripting Guy! forum, and he has been a judge for the Scripting Games. He has presented talks on the topics of WSUS and PowerShell as well as runspaces to PowerShell user groups. He is an Honorary Scripting Guy, and he has submitted a number of posts as a to Microsoft's Hey, Scripting Guy! He also has a number of open source projects available on Codeplex and GitHub. His personal blog is at http://learn-powershell.net.

comments powered by Disqus
Most   Popular