Regular Expression Creation Tool

Regular expressions are very powerful and often useful in a variety of situations. If like me you find it difficult to remember the syntax and parameters when creating regEx this website will be a fantastic aid – https://regexr.com/

Here we have a screenshot of the initial page with some sample text and a basic expression. Of note is the side bar to the left with links to a cheatsheet and other reference material.

RegExr Learn Build & Test RegEx

RegExr Cheatsheet

 

A colleague of mine was creating a PowerShell function which required input verification for a parameter. He was expecting the user to input a MAC address and wanted to ensure only valid characters and syntax for the data. He came up with the following which I use here as an example –

([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})

The expression describes a pattern of 2 characters, with valid options being lower and upper alpha a – f and numeric. A : or – is accepted as a value between the two characters and this is to be repeated 5 times with the final pattern being just two characters from the valid set. Note below that the first two examples are matched as expected but the third fails to match due to the use of a ‘h’ character which is not valid in the hexadecimal format of MAC addresses and as defined in our regular expression.

RegExr MAC Address Examples

 

The website offers a great way to build and test regular expressions with feedback on what is happening and why as you go. The ability to save and share expressions can also be useful. I’m sure many people will find this beneficial and hopefully over time the use of this site will instil confidence in regEx usage and techniques.

2 thoughts on “Regular Expression Creation Tool”

Leave a Reply to frSysadminCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.