Regex is cool! It is true! Regex allows you to find specific chunks of text in a known sentence, file, document etc… Regex is really useful to parse big log files, to identify a specific pattern, and to turn non structured data into structured data. Regex can also be used in powershell to validate user input for instance.

I use regex to do both. But prior to start the parsing, we first need to learn the syntax of regex. So let’s start!

Using regex can seem to be pretty difficult because of its ‘particular‘ syntax. And I agree, each time you are reading a regex, you are taking the risk of losing the sight of an eye due to its particular syntax. (Ever wonder how cyclops from the X-men got his cool glasses? I bet he spent a bit too much time reading regex at night in the dark!). Some things can really throw you off. But believe me, there is actually a very little to learn, to start to write really powerful and useful regexes.

I tried to cover the fundamentals, and the most use cases in 13 points to learn regex.

The 13 points to learn REGEX contain EVERYTHING you need to know about regex to get you started with the language.

On the road, I’ll try to pin point out the tricky parts, so that the learning process is as easy as possible for you.

Why should you even learn regex?

First, because I don’t like to have chunks of characters that I don’t understand the meaning of in my code.

Second, because the whole new possibilities that they give us concerning log parsing and user input validation.

And lastly, because Regex can be used in a lot different languages. So learning regex today definitely be a huge gain on the long term. The concepts you learn here today, will be applicable to many different programming languages (Powershell, C#, php, Javascript etc…), so you are gaining on all levels by learning regex.

Once you are comfortable with regex, head over to this article where I cover the major points on how to use efficiently regex in PowerShell.

Regex format

Before we even start to look at the regex syntax, let’s have a look at what the basic structure of a regex is:

SelectorQuantifier

The two words above are stuck together on purpose. Using regex, you first have to use a selector then apply (or not) a quantifier to the selector to multiply it. The selector will actually select the character you are searching for, and the quantifier says how often that character must be repeated. Together they will match (or not) a specific portion of a string.

In the end, a long regex is nothing else then a selector with a quantifier repeated over and over and over again, as showcased in the example below:

SelectorQuantifierSelectorQuantifierSelectorQuantifier

It is also possible to create grouping around our selectors to address entities that below together as a whole. I cover groupings a bit lower in this article, but it makes sense to first learn the basics of the Regex syntax.