To make that happen, we'll use PowerShell's replace operator. The replace operator to find complex strings, use regular expression groups. Summary: Use Windows PowerShell to replace non-alphabetic and non-number characters in a string. How can I use Windows PowerShell to replace every non-alphabetic and non-number character in a string with a hyphen? Use the Windows PowerShell –Replace operator and the w regular expression character class.
Are you familiar with PowerShell's -replace operator?
Most folks are aware of it, and rely on it for straightforward string replacements like this one. But not very many people know that -replace also does some amazing stuff using regular expressions.
That'd change the input string to '192.168.10.12,192.168.10.8,' replacing all occurrences of two digits, between periods, to 10. The 12 would be skipped because it isn't followed by a period, as specified in the pattern. Note thatテつxA0all occurrences are replaced, in keeping with the usual operation of -replace.
The operator can also do capturing expressions, and this is where it gets really neat-o.
Here, I've specified two capturing expressions in parentheses, with a space character between them. PowerShell will capture the first to $1, and the second to $2. Those aren't actually variables, which is important. In my replacement string, I put $2 first, followed by a comma, a space, and $1. The resulting string will be 'Jones, Don'. It's important that my replacement string be in single quotes. In double quotes, the shell will try and treat $1 and $2 as variables, instead of using them as captured regex placeholders. I kinda wish they'd used something other than a $ for the captured placeholders, so that they didn't look like variables, but the syntax is in keeping with regex standards.
I think it's cool to see all the places a regex can be put to use. The -split operator also supports regex syntax as a way of specifying the separator that will be used to break a string into components, so you're not limited to splitting just on a single character like a comma.
Apart from the well-known -match operator and the Select-String command, where else have you used a regex in PowerShell?
This topic contains 6 replies, has 3 voices, and was last updated by
Recco Bucceri
3 years ago.- I would remove the brackets () and escape the dot with a backslash. You don't really need the brackets to create a group in this case.$DisplayName -replace '_[0-9].pst$'
- And replace the double quotes with singles or escape the dollar-sign with a backtick.$DisplayName -replace '_[0-9].pst$'$DisplayName -replace '_[0-9].pst`$'
- I agree with the improvements that Daniel mentioned. However, your regex does remove the .pst for me and it should for you. Can you post the regex in context of the surrounding code?
- Apologies for my delayed reply. I've been on the move quite a bit since September 6th without access to a PC.() are used in regular expressions to define a capturing group to extract data. You can read more about capturing groups here: http://www.regular-expressions.info/refcapture.htmlPowerShell Example:
- Posts
The topic ‘Regex - replace’ is closed to new replies.