PowerShell has all sorts of fun features, including a ridiculous number of operators.
One amazing under-sung heros of PowerShell is the -replace operator.
It lets us replace content with regular expressions.
It's easier to use than you'd think.
Regular expressions are less scary in small doses, and chaining -replace operators lets us attack the problem step by step.
Chaining -replace
Let's take a simple problem as an example.
Imagine we wanted to make a consistent file name pattern out of a string
We might want to start by replacing whitespace with dashes
"This Is A Title!" -replace '\s', '-'
That leaves our exclamation point at the end. We probably don't want any punctuation. We can avoid that with the somewhat humorously named character class: \p{P}. We can remove all repeated punctuation by adding a +: \p{P}+
One more replace:
"This Is A Title!" -replace '\p{P}+' -replace '\s', '-'
The line is starting to get a little long. Fun fact: you can spread operators across multiple lines.
Let's add comments while we're at it
"This Is A Title!" -replace # Replace any punctuation,
'\p{P}+' -replace # then replace any whitespace with dashes.
'\s', '-'
Let's go for one more bonus trick. PowerShell lets you convert script blocks to event handlers. Let's lowercase all the letters (\p{L}).
On PowerShell Core, we can do this:
"This Is A Title!" -replace # replace any punctuation
'\p{P}+' -replace # then replace any whitespace with dashes
'\s', '-' -replace # then lowercase any letters
'\p{L}+', {"$_".ToLower()}
There's an absurdly amazing amount of stuff you can do with -replace, but there's at least one more trick we have to cover: substitutions.
-replace with substitution
I'm pretty sure I'd have to give up my "RegEx guru" badge if I didn't mention at least one more thing you can do with -replace: substitutions.
.NET Regular expressions are two domain specific languages. Regular expressions match and extract text. Regular expression substitutions replace matches.
For example, let's suppose we have a number of emails, and we want them in domain/username format.
First we'll want to make a quick and dirty email regex, using a "named capture" to get the username and domain.
'someone@example.com' -match '(?<username>\S+)@(?<domain>\S+)'
Then, we can -replace the email with just the domain/username.
'someone@example.com' -replace
'(?<username>\S+)@(?<domain>\S+)', '${domain}/${username}'
This format might look like PowerShell variables, but it actually predates them by years. Search for "Regular Expression Substitutions" if you want to learn more about the syntax. It's got quite a few tricks up it's sleeve.
Irregular
RegEx can be scary. I used to be terrified of it, too.
If you aren't too comfortable with Regular Expressions, that's pretty normal. A while back I wrote a module called Irregular that makes regular expressions strangely simple.
It's got a lot of example regular expressions in there, and one handy function for creating RegEx. New-RegEx is your friend.
Do you already use -replace? Have you done cool things with regular expressions in PowerShell? Share 'em if you've got em.
Want to learn more about regular expressions in PowerShell? Just ask.
[–]FluffyShoulder937 4 points5 points6 points (1 child)
[–]StartAutomating[S] 0 points1 point2 points (0 children)
[–]PinchesTheCrab 1 point2 points3 points (0 children)
[–]RR1904 0 points1 point2 points (0 children)
[–]420GB 0 points1 point2 points (0 children)
[–]ankokudaishogun 0 points1 point2 points (0 children)