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.
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.
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.
I always learn towards using regex101.com. :)
Thanks for the comment frSysadmin, that looks like another great site for both testing and reference purposes.
Alex