Hopefully, everyone is still staying safe.
I have kept myself busy with more and more work within the company to be much impacted by the current situation.
Anyway, time for some timesaving additions to our Powershell scripts. Shortcuts that allow us to reduce even a single action by up to 3 lines.
I am talking about... Regex, also known as Regular Expressions.
Regular Expressions are short aliases and patterns that allow us to easily match variables, strings and more without having to write extensive lists of comparison materials (More on this will be made clear soon).
In this post I will go through the basics of this system.
The most used methods of interacting with regex is through -match, -split and -replace operators. These operators do not differentiate normal letters from capital letters.
If you want to make a match or replace only to happen when even the capital letters are exactly the same, you can add a "c" in front.
Meaning the operators that will also match capital letters are -cmatch, -csplit and -creplace.
Literal characters (Example 1)
As Test contains est, it will give back a true
Character groups and ranges (Example 2)
Character groups and ranges allow you to match depending on variable characters within the string or character.
With the first line, it tries to match Powershellder with a string whereby the “e” got replaced by square brackets containing multiple characters. If that spot contains any of those characters, it will give back true. So it would have also given back a positive result if the first string was ‘Powershillder’ or ‘Powershollder’ to.
When the group starts with a “^”, it means the -match or -replace will take everything EXCEPT for the character or range group. So with the example on the second line, it will give true UNLESS it's either ‘Powershellder’, ‘Powershillder’ or ‘Powershollder’.
The third and fourth line take above concepts and use it with a range. Whether it's an alphabetical or numerical range does not matter. In the case of the third line it matches ‘Test4’ with a Test that ends on anywhere from 1 to 5. As 4 is within that range, it gives back true.
The fourth line is the other way around.
This can also be done with multiple ranges or regex as seen in the fifth line.
Numbers (Example 3)
The regex class for a decimal digit is \d.
\d simply stands for ANY decimal digit.
One thing to remember with these expressions is that their capital letter forms do the opposite.
This means that \D stands for anything EXCEPT for decimal digits.
As seen in example #3, you can use \d to match strings whereby the decimal digits within the string could vary or \D if there should not be a digit at a certain spot.
The usage of \ only looks at the first character behind it so I can use it in the middle of text as seen in the third line.
Words (Example 4)
\w refers to any word-characters. This means a-z, A-Z and 0-9. \W thus refers to non-word characters.
In the first line can be seen that due to Test1 containing word-characters, it ends up being true.
The second line only exists of non-word characters and thus gives false.
When using \W, it ofcourse does the opposite of \w.
Wildcards (Example 5)
You can use a wildcard by using the “.” character. This allows you to wildcard 1 character per “.” .
In line 1 it can be seen that when testing whether or not “Test1“ contains 5 wildcards worth of characters, it shows as true due to it having 5 characters.
When tested against 6 wildcards however (the second line), it returns false. This is due to it not being able to contain 6 wildcards, no matter what we replace them with.. as “Test1“ is only 5 characters long.
The third line shows us using it within a string in a more logical sense.
There is only 1 exception that the wildcard does not match to and this is a newline (when a string contains “\n”)
Whitespaces (Example 6)
Whitespaces are matched with the \s regex. Like previous, non-whitespaces are matched using \S.
The first line shows an example on how this can be used to match strings.
\s is also very usefull in combination with replace.
And that was it for the first part!
Next part will be focussing on how to use all of the operators, how to control the amount of regex within a string and more.
Hopefully this will bring some more understanding in regards to these very useful tools.
Categories: Powershell, Information, Basics, Regex
Patrick Berger AKA Powershellder.
[ i ] Parallax section below. Click on the section below to upload image. Don't worry if it looks weird in the Weebly editor. It'll look normal on your published site.
To edit or delete your image, press the "toggle" button below. Then, hover over your image until a popup appears with the "edit" and "delete" options. If you don't want a white content section, leave it blank. It will disappear on your live website.