Monday, April 24, 2017

Powershell Tip: Escaping special Characters in a String using [RegEx] type accelerator

Powershell Tip: Escaping special Characters in a String using [RegEx] type accelerator

http://ift.tt/2pd2nnf

Often when using the -Replace (Operator) or .replace() (Method) in Powershell, I forget that the former parses the string in regex and in case you’re passing Special characters in the string, you’ll get the error all over your console.

That is because some special characters are part of regular expression language and are considered are Meta Characters in RegEx, so it’s always a best practice to escape special characters.

To your rescue,  here is a quick tip to escape all special characters in a string using the .Net Type accelerator for Regular expression [RegEx] , something like in the following image –

regex

and an animation with a use case.

tip

Hope you’ll find this useful, until next time, Cheers!

signature


Filed under: Powershell, PSTip, Today I Learned ! Tagged: .Net, dotnet, Powershell, PSTip, Regex, Regular expression, String, TypeAccelerator

Powershell

Powershell

via Planet PowerShell http://ift.tt/2ozzxeJ

April 23, 2017 at 10:51PM

No comments:

Post a Comment