[英]Matching Variables using Powershell Match
I need to parse some config files and find a match. 我需要解析一些配置文件并找到匹配项。 I've simplified the problem to this. 我已经简化了这个问题。 Why doesn't powershell match the backslash even when it's been escaped? 为什么Powershell即使逃脱也不匹配反斜杠? It works if I remove the backspace from the code below. 如果我从下面的代码中删除了退格键,它将起作用。
Find $b in $a: 在$ a中找到$ b:
$a="lorum [\test] ipsum"
$b="[\test]"
([regex]::Escape($a )) -match ([regex]::Escape($b))
It's because you're escaping the string itself. 这是因为您要转义字符串本身。 The part before -match
should be a normal string, while the part after needs to be a regex pattern. -match
之前的部分应为普通字符串,而after之后的部分应为正则表达式模式。 What your command actually does is comparing these strings(think of them as normal strings): 您的命令实际上所做的是比较这些字符串(将它们视为普通字符串):
"lorum \[\\test\] ipsum" contains "[\test]"
Which is never true. 从来没有这样。 Try matching the string $a
to the escaped pattern $b
: 尝试将字符串$a
与转义的模式$b
匹配:
$a="lorum [\test] ipsum"
$b="[\test]"
$a -match ([regex]::Escape($b))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.