使用 Powershell 在字符串中搜索反斜杠

Searching for backslash in a string with Powershell

我需要搜索字符串中反斜杠的数量以确定某些文件路径参数。我还没有想出一种方法来搜索反斜杠,而 Powershell 认为它是一个逃逸字符。

([regex]::Matches($FilePath, "\" )).count 

$a -match "\"

这两个都报错"Illegal \ at end of pattern"

谢谢!

您可以使用反斜杠转义反斜杠:

[Regex]::Matches($FilePath, "\").Count