PowerTip: How to escape characters in a string using regular expressions

Doctor Scripto

Dr Scripto

Summary: Cloud & Datacenter Management MVP, Thomas Rayner, shows how escape characters for use in regex.

Hey, Scripting Guy! Question Is there a way to escape characters in a string automatically for use in regex?

Hey, Scripting Guy! Answer Use the [regex]::escape() method to automatically escape characters in a string so you can use them in regex.

[regex]::escape('something. in here (needs) escaping')

something\.\ in\ here\ \(needs\)\ escaping

The Doctor

Doctor Scripto
Dr Scripto

Scripter, PowerShell, vbScript, BAT, CMD

Follow Dr Scripto   

0 comments

    Leave a comment