How To Use Regular Expressions To Match Strings In PowerShell
If you ever need to match a pattern using PowerShell, the most efficient way to do so is to use a Regular Expressions. Except that regular expressions are complicated, difficult to use, and a nightmare to debug… Or are they? Maybe it is time to try them out!
In this snip, Anthony will get you started using regular expressions in PowerShell to match a pattern in a string. In this case, he’ll walk through matching a number in a string, but only matching numbers that are less than 200. This will be a quick introduction to using ‘\d’ with the -match operator.
Prerequisites include: PowerShell
if you need to find a pattern inside of a string most effective way to do that is regular expressions and so in powerShell there's an operator called match and that will match a regular expression to a string and evaluate true or false? So here in line four I'm matching string to string just to kind of demonstrate a really simple example. And you see this, if I was true but you know it's the same thing as if you use the like operator here? That also lightweights true so that's not a great example. So if you put a number inside of that string so get string with a one and 7 I. But I want to see if there's a number in that string. We match operator in these slash D regular expression character, which evaluates to any number so slash to use any number so it should go true because there's a one in that string, but we see that string. We know that the number one is what it matched on. In PowerShell nose and stores that in the matches variable so if we look at the matches. Variable we can see that the value is one and so that variable is updated every time the match operator is used. So we scroll back up here. Revaluate, string string. And look at the matches variable again. We see that the value is now string. By default it calls the first value zero. And so we can reference that specifically using these bracket notation here. And return just that value. And its useful if you need to find the matched value in your script or something. So let's take a look at a more practical example. So here I've got a sentence I've worked in anti for 30 years and I'm assigning it to these string variable and keep in mind that I have not work tonight for 30 years that and so, if we want to find the numbers out of there. We can use that slash D to match see if there's a number on there and then all the valuate to true and senses inside of an if statement it will also run blind 30, the matches and look at the value. And you can see down here. We've got 3 well. That's not 30 that's not all of the numbers. So there's a way to get more of the numbers so hairline 35. I got slash D plus so the plus character in regular expressions qualifies the preceding character and tells the regular expression engine to get one or more of that number, so we get as many as it can so in this case. That will actually return 30 'cause it will match numbers until it runs out of number. So we got 3. The zero and the space wasn't a number so it stopped. But the issue, there is what, if someone puts I worked 19 for 3000 years. Instead of 30. Let me show you so that matched 3000. So then we need to do is define acceptable range can only match on acceptable ranges. So I am assuming that no one will have more experience than 199. You could probably go a lot less than this. But just for the sake of this demo. I'm just trying to keep it simple I'm assuming the oldest person in the world was 120 130. An maybe they had hundreds of experience so for the sake of this demo. I'm assuming that number won't be bigger than 199. Yeah, you probably argue that that's too high, but I wanted to add the one in the beginning to kind of show a more complicated example, so in this case on line 48. My regular expression here. I've got one inside of parentheses followed by the question mark, so the question mark qualifies the previous character and says. One or none of this character. And then immediately following that I have this slash D which is member numbers and then the one comma 2 inside the curly brackets that qualifies that slash D and says, we want to get one or 2 of this number, but not 3 and not 01 or 2. So that's what that will match on so if we look at this with our 3000 number. It still matches 30 because that's 2 numbers remember, we could add a space here and then it doesn't match but spaces in regular expressions. They kind of work me up a little bit. So we can do to make that even better, so I got a better matching section. We can use slash Capital D, So what the slash capital. The will do is that matches anything that's not a number so slash Lowercase D is a number of slash uppercase D is anything that's not a number so in this case. This will match on anything has a one or not a number at the beginning followed by one or 2 numbers followed by not a number. So this will this match 3000. No, it does not, but will a match 130? Find out. Yes, it does. Will this match 201? No does not remember 199 was the top. And so if we get a match to. Ah, yes, it does, but one of the things you'll notice is that there's a space that begin to this because we're matching that space in front of the numbers so the non numeric number. The Slash Capital D. So we can even look at here. Matches zero dot length and you can see that that's actually 3 characters long and it's only the one number. Trim will save the day here will do matches zero dot trim. And there, we go, we got a standard output, so that's how you can get started matching with regular expressions in powershell. Thanks for watching.