将 -SimpleMatch 与 Select-String 一起使用时未捕获匹配项

Matches not captured when using -SimpleMatch with Select-String

我一直在使用正则表达式来解析某些 XML 节点中的文本。但是,当我将 -SimpleMatchSelect-String 一起使用时,MatchInfo 对象似乎不包含任何匹配项。

我无法在网上找到任何表明此行为正常的信息。我现在想知道这是否是我的 Powershell 安装。 (供参考:我使用的电脑安装了Powershell 3.0。)

使用一个非常简单的示例,我们可以 return 使用正则表达式模式时预期的 MatchInfo 对象:

PS H:\> $c = "abd 14e 568" | Select-String -Pattern "ab"

PS H:\> $c.Matches


Groups   : {ab}
Success  : True
Captures : {ab}
Index    : 0
Length   : 2
Value    : ab

但是添加 -SimpleMatch 参数似乎不是 return MatchInfo 对象中的匹配 属性:

PS H:\> $c = "abd 14e 568" | Select-String -Pattern "ab" -SimpleMatch

PS H:\> $c.Matches

PS H:\>

管道 $cGet-Member 确认 MatchInfo 对象被 returned:

PS H:\> $c | gm


   TypeName: Microsoft.PowerShell.Commands.MatchInfo

Name         MemberType Definition                                                       
----         ---------- ----------                                                       
Equals       Method     bool Equals(System.Object obj)                                   
GetHashCode  Method     int GetHashCode()                                                
GetType      Method     type GetType()                                                   
RelativePath Method     string RelativePath(string directory)                            
ToString     Method     string ToString(), string ToString(string directory)             
Context      Property   Microsoft.PowerShell.Commands.MatchInfoContext Context {get;set;}
Filename     Property   string Filename {get;}                                           
IgnoreCase   Property   bool IgnoreCase {get;set;}                                       
Line         Property   string Line {get;set;}                                           
LineNumber   Property   int LineNumber {get;set;}                                        
Matches      Property   System.Text.RegularExpressions.Match[] Matches {get;set;}        
Path         Property   string Path {get;set;}                                           
Pattern      Property   string Pattern {get;set;}                                        

以及其他属性,例如图案和线条工作:

PS H:\> $c.Pattern
ab

PS H:\> $c.Line
abd 14e 568

此外,将索引值发送到 Matches 数组时不会生成错误:

PS H:\> $c.Matches[0]

PS H:\> 

我不确定如何解释结果,也不确定为什么会这样。

这种行为是有问题的,因为很多时候我必须搜索包含正则表达式特殊字符的字符串,() 很常见。

扩展示例:

PS H:\> $c = "ab(d) 14e 568" | Select-String -Pattern "ab(d)"

PS H:\> $c.Matches

PS H:\> 

$c.Matches return 什么都没有,并且 $c 本身是空的,因为在正则表达式模式中使用了括号:

PS H:\> $c -eq $null
True

使用 -SimpleMatch 确实会生成 MatchInfo 对象,但仍然不会 return 任何匹配项:

PS H:\> $c = "ab(d) 14e 568" | Select-String -Pattern "ab(d)" -SimpleMatch

PS H:\> $c -eq $null
False

PS H:\> $c.Matches

PS H:\> 

我找到的解决方法(在此处)是使用 .NET 中的 Regex.Escape 方法:
(参考:Powershell select-string fails due to escape sequence

PS H:\> $pattern = "ab(d)"
$pattern = ([regex]::Escape($pattern))
$c = "ab(d) 14e 568" | Select-String -Pattern $pattern

PS H:\> $c.Matches


Groups   : {ab(d)}
Success  : True
Captures : {ab(d)}
Index    : 0
Length   : 5
Value    : ab(d)

由于此解决方法 return 与 Select-String 的预期匹配,我已经能够继续编写脚本。

但我很好奇为什么在使用 -SimpleMatch 参数时没有匹配 returned。

...
关于,
施韦特

来自Get-Help Select-String -Parameter SimpleMatch

-SimpleMatch [<SwitchParameter>]

Uses a simple match rather than a regular expression match. In a simple match, Select-String searches the input for the text in the Pattern parameter. It does not interpret the value of the Pattern parameter as a regular expression statement.

因此,SimpleMatch 只是在您通过管道传递给它的每个字符串中对 $Pattern 进行子字符串搜索。它 returns 一个包含字符串和相关上下文信息(如果存在)的 MatchInfo 对象,但没有 Matches 因为从未对字符串执行正确的正则表达式匹配 - 就这么简单