powershell 比较带双引号的字符串

ix0qys7i  于 2022-12-23  发布在  Shell
关注(0)|答案(1)|浏览(227)

我想比较一个带双引号的字符串。我试了下面的代码,但是没有用。
样本$inputStr'{"to":"[Email]","from":"no-reply@mirvac.com","cc":"","bc":""'

$inputStr = '{"to":"[Email]","from":"no-reply@email.com","cc":"","bc":""'
$emailStr = '"[Email]"'
if ($inputStr -like "*"+$emailStr+"*") {
   write-host "exists"
}

有什么建议吗?
谢啦,谢啦

2lpgd968

2lpgd9681#

比较不起作用,因为[]wildcard characters,需要使用反勾号```('"[Email]"')或WildcardPattern.Escape method进行转义:

$inputStr = '{"to":"[Email]","from":"no-reply@email.com","cc":"","bc":""'
$emailStr = '"[Email]"'
if ($inputStr -like "*$([WildcardPattern]::Escape($emailStr))*") {
   Write-Host "exists"
}

相关问题