如何比较powershell中包含问号(?)的字符串
我有下面的代码片段。使用-match命令进行比较时失败如何比较powershell中包含问号(?)的字符串,powershell,Powershell,我有下面的代码片段。使用-match命令进行比较时失败 $string1 = 'abc?v' $string2 = 'abc?v' if ($string1 -match $string2) { Write-Output "Matched" } 我需要比较我的应用程序中的weburl,weburl可能包含?。使用-like代替-match以获得所需的输出 如果需要文字比较,应使用-eq比较运算符-match使用正则表达式引擎进行比较,其中?将
$string1 = 'abc?v'
$string2 = 'abc?v'
if ($string1 -match $string2)
{
Write-Output "Matched"
}
我需要比较我的应用程序中的weburl,weburl可能包含?。使用-like代替-match以获得所需的输出 如果需要文字比较,应使用
-eq
比较运算符-match
使用正则表达式引擎进行比较,其中?
将之前的标记比较0或1次,而另一个答案中的-like
是一个通配符运算符,其中[]
、?
和*
被解释为特殊
if ($string1 -eq $string2) {
查看
[regex]::Escape()
函数。-like
和-match
做两件不同的事情;它们不能互换-match
使用正则表达式,部分匹配不一定需要通配符-like
使用简单的通配符,并且必须具有用于部分匹配的通配符。正如帖子中所说,这个问题并没有提供足够的上下文来了解像-like
这样的问题是否是一个可行的选择,而且问号是一个简单的通配符,所以即使是像-like
这样的问题也可能有问题。@JeffZeitlin在帖子中,他们在查看URL,所以他们要的是一个查询字符串。我为该作业发布了正确的运算符,但我仍然觉得这是XY问题。@TheIncorgible1-您可能对XY的看法是正确的-并且可能对使用-eq
的看法也是正确的。不过,在发布任何我认为是答案的东西之前,我想从查询中得到更多的信息。