Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

-match fails when checking if a string contains a path

I have a list of strings, and need to check each item to see if it contains some string $path, where the string should contain a unc path and $path is also a unc path.

For example:

"RW \\test" -match "\\test"

returns True, as \\test is contained in RW \\test. Great.

So why does this return False ? :

"RW \\test\te" -match "\\test\te"

At first I though maybe the single backslash is somehow acting as an escape character (even though in PowerShell that should be `)

So I tried

"RW \\test\\te" -match "\\test\\te"

But this also returns False ....

Why?

like image 321
Bassie Avatar asked Dec 31 '25 13:12

Bassie


2 Answers

To complement Trevor Sullivan's helpful answer with a tip provided by PetSerAl in a comment on the question:

To use a string as a literal in a regex context, pass it to [regex]::Escape():

PS> "RW \\test\te" -match [regex]::Escape("\\test\te")
True

[regex]::Escape() conveniently escapes all characters that have special meaning in a regex with escape character \, so that the string is matched as a literal:

PS> [regex]::Escape("\\test\te")
\\\\test\\te

Note how the \ instances were each escaped with \, effectively doubling them.

If your string does use regex constructs but also contains characters with special meaning in regexes that you want to be treated as literals, you must \-escape them individually:

PS> '***' -match '\**'  # match zero or more (*) '*' chars (\*)
True
like image 151
mklement0 Avatar answered Jan 03 '26 23:01

mklement0


You need to escape both of the backslashes with backslashes in your regular expression on the right-hand side of the -match operator.

PS /> "RW \\test\te" -match "\\\\test\\te"
True

Here's what the result looks like:

PS /> $matches[0]
\\test\te

You could also expand on this to use named captures in regular expressions. Named captures just give friendly names to individual captures inside of a regular expression, making them more easily referenced as a property on the $matches variable, instead of a numeric index.

PS /> "RW \\test\te" -match "(?<UNCPath>\\\\test\\te)"
True
PS /> $matches.UNCPath
\\test\te

Keep in mind that the backtick character is used to escape certain special characters in PowerShell double-quoted strings. However, in the case of the -match operator, you're invoking the .NET regular expression engine. In the .NET regex engine, the backslash is used to escape special characters in the regex context. Hence, in this example, the backtick escape character isn't applicable.

Also, make sure that you are not escaping special characters in your source string, on the left-hand side of the -match operator. The reason that your final example doesn't match, is because you added a second \, but only escaped a single \ in the regex on the right-hand side of the -match operator.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!