I invite you to follow the Scripting Guys on Twitter and Facebook. Thanks, Thomas! You’ve really got my interest charged up with regex! Time to see what fun I can have! Join me next week, and we’ll talk about grouping and character classes!
The pattern of three digits only occurs once, and so that is the value that gets returned. The difference here is that the pattern I’m looking for is three digits, not just one. ::matches('abc123','\d\d\d').value #returns 123 in a stringĪt first glance, this looks very similar. I’ll get an array back of all the matches in that string. By default, lots of information is returned, and I’m just isolating the value property. The first parameter is the string to look within, and the second is the regex pattern to match. Here, I’m looking for the digits in “abc123” using the matches() method of the accelerator. What about if you want to get the specific value in a string that matches a pattern? Let me show you. So far, we’ve been working with boolean responses (true or false) to tell if a string matches a specific pattern. There’s a accelerator that offers a whole bunch of functionality. Now, I’d like to introduce another mechanism in PowerShell to work with regex. You can start to see how you can string these patterns together to make more elaborate patterns. The second one is true because it means “any number followed by the end of the string”, which is clearly present in our “something123” example. The first one is false because the pattern is “the start of the string, followed by any number”, and that obviously doesn’t match our string. 'something123' -match '\d$' #returns true 'something123' -match '^\d' #returns false The pattern here is “the letters s o m e, any character, h i n g 1 2 and then any number”, which is a match for our string “something123”. 'something123' -match 'some.hing12\d' #returns true matches a dot and Error! Hyperlink reference not valid. ‘ ‘ -match ‘\S’ #returns false because all the characters are whitespace ‘ ‘ -match ‘\s’ #returns true because between the quotation marks is a single space ‘hello123’ -match ‘\W’ #returns false because everything is an alphanumeric character ‘hello123’ -match ‘\w’ #returns true because alpha numeric characters are present ‘1234’ -match ‘\D’ #returns false because everything in the string is a number ‘testing123’ -match ‘\d’ #returns true because there are numbers present Works just like the newline except for tabs instead of new lines Line -match ‘\n’ #returns true because there is a new line between ‘multi’ and ‘line’ ‘something’ -match ‘some.hing’ #returns true because the ‘t’ is a single character that matches this pattern Let’s jump into some different special characters and some examples. Neat, right? You can use these special characters to unlock the power of regex and start to create more complex patterns. For example, \d is used to match “any digit”. These characters within regex have a different meaning than you might assume. Today, I am going to introduce special characters.
For the uninitiated, big strings of seemingly random characters appear indecipherable, but regex is an incredibly powerful tool that any PowerShell pro needs to have a grip on. By the end of this series, you’ll have the skills to identify that pattern matches IP addresses. If you see something like ‘(\d)’ and your eyes start glazing over, don’t worry.
Many people are intimidated by regular expressions, or “regex”.
This isn’t meant to be a comprehensive series but rather, just as the name says, a crash course. Regular expressions are extremely useful to extract information from text such as log files or documents. Regular expressions are sequences of characters that define a search pattern, mainly for use in pattern matching with strings.
This week, I’m presenting a five-part crash course about how to use regular expressions in PowerShell. You can find me on Twitter ( or posting on my blog,. Hello! I’m Thomas Rayner, a proud Cloud and Datacenter Management Microsoft MVP, filling in for The Scripting Guy! this week.
Summary: Thomas Rayner, Microsoft Cloud and Datacenter Management MVP, shows the basics of working with regular expressions in PowerShell.