My apologies for the lengthy silence, but it has been very busy in my life.
Holidays, several important matters like the purchase of a house and a ton of work at my job have been keeping me busy.
It is important to finish what I started though so part 2 of the basic use of regex (Regular Expressions).
Multiple characters (Example 1)
A “+” can signify that there can be multiple of the preceding character. This can also be a regex.
In Example 1 we have multiple numbers after a word. By specifying a “+” after the \d we make sure that it does not matter how many numbers come after the word.
This allows for multiple interesting options including use with numbers that can change format.
With the \W and \w we can prove that it goes through the numbers and the regular expressions can continue after a regex with “+”.
First and final character (Example 2.1 and 2.2)
Using ^ and $ we can check the first and final characters of anything we want to match.
By putting ^ in front of a character or regex, we can match whether something starts with specified character(s). We can see this in Example 2.1.
This makes it so you for example can filter out text that does not start with a specific character. Like filtering out any text not starting with T when you need to retrieve a list of ticket numbers starting with T.
By putting $ behind a character or regex, we can check whether or not something ends with a specific character or regex. It works the same way as above command, except targets the last character(s) as seen in Example 2.2.
Split using -match (Example 3)
Example 3 shows us we can use round brackets to split variables etc. with -match.
By using the brackets, it matches the characters and Regular Expressions within the brackets separately which leads to be able to split the matched parts.
We can then use the $matches.0 (everything), $matches.1 (First part) and $matches.2 (second part) to get the matched parts separately.
Split using Regex (Example 4)
Regular Expressions can also split up variables etc. In example 4 we can split up a string containing both alphabetical and numerical characters by targeting any digit as a point of splitting. This can make use of the Regular Expressions, strings and more.
While I have not used it in many scripts yet, Regular Expressions seem to make more and more impact on the ones I do use it for, so I can certainly recommend doing even deeper investigation on the matter.
Hopefully this has made a few more Regular Expressions clear to others.
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.