Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String PowerShell问题与字符串匹配_String_Powershell_Match - Fatal编程技术网

String PowerShell问题与字符串匹配

String PowerShell问题与字符串匹配,string,powershell,match,String,Powershell,Match,我需要将字符串的内容与字符串集进行匹配。我有这样的想法: >$ID = "GEt" >$ID -Match "Get|YES|NO" True 我不需要-cmatch-没关系。但以下情况并非如此: >$ID = "targetService" >$ID -Match "Get|YES|NO" True 如果我要查找的字符串是另一个字符串的子字符串,如何避免这种情况?可以通过添加开始和结束行定位(^和$)强制与交替正则表达式精确匹配。使用非捕获组将替换文本与定位点隔

我需要将字符串的内容与字符串集进行匹配。我有这样的想法:

>$ID = "GEt"
>$ID -Match  "Get|YES|NO"
True
我不需要
-cmatch
-没关系。但以下情况并非如此:

>$ID = "targetService"
>$ID -Match  "Get|YES|NO"
True

如果我要查找的字符串是另一个字符串的子字符串,如何避免这种情况?

可以通过添加开始和结束行定位(^和$)强制与交替正则表达式精确匹配。使用非捕获组将替换文本与定位点隔离:

$ID = "targetService"
$ID -Match  '^(?:Get|YES|NO)$'

False
同样,当您在一组字符串中查找精确匹配时,不需要执行regex
-match
操作。只需使用PowerShell 3.0+中的(或
-in
):


“获取”、“是”、“否”-包含$ID
问题?不这是Regeeeex!说真的,您应该在powershell中阅读一些关于正则表达式和匹配的内容。
PS C:\> $ID = "GEt"
PS C:\> $Options = "get","yes","no"
PS C:\> $Options -contains $ID
True
PS C:\> $ID -in $Options
True
PS C:\> $ID = "targetService"
PS C:\> $Options -contains $ID
False