PowerShell Tip - Escape Regex MetaCharacters
Last week I worked on a Scorch PowerShell script that is looking for duplicate Incident Requests inside SCSM by checking new incoming request and existing ticket already in the system.
One of the script step is to look for a match between two strings, something similar to the following:
$String1 = "Title:[PowerShell Rocks!]"
$String2 = "Title:[PowerShell Rocks!]"
$String1 -match $String2
Straight forward you would think! And at first I was surprised to see the result $FALSE
… yep…to “ass-u-me”…
What !? |
After taking another golp of coffee… I realized that the match operator was interpreting the :
,[
and ]
as regex metacharacters.
Using BackSlash character to ignore metacharacters
I could easily escape those characters by placing a backslash \
character to ignore those
$String1 = "Title:[PowerShell Rocks!]"
$String2 = "Title:\[PowerShell\ Rocks!]"
$String1 -match $String2
Using the [Regex]::Escape() method
Since my script might received very different type of text inputs with some other metacharacters, another technique is to use the method ESCAPE() that comes with the System.Text.RegularExpressions.Regex class, we can use the [regex] accelerator type to call it.
$String1 = "Title:[PowerShell Rocks!]"
$String2 = "Title:[PowerShell Rocks!]"
# Using the full classname
$String1 -match [System.Text.RegularExpressions.Regex]::Escape($String2)
# Using the Accelerator type
$String1 -match [Regex]::Escape($String2)
If you look on MSDN documentation, you find the following details:
Regex.Escape Method Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space) by replacing them with their escape codes. This instructs the regular expression engine to interpret these characters literally rather than as metacharacters.
Leave a comment