将-SimpleMatch与Select-String

时间:2015-10-16 21:20:50

标签: regex powershell escaping select-string

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

我无法在网上找到任何表明此行为正常的内容。我现在想知道它是否是我的Powershell安装。 (供参考:我使用的计算机安装了Powershell 3.0。)

使用一个非常简单的例子,我们可以在使用正则表达式模式时返回预期的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参数似乎不会返回MatchInfo对象中的Matches属性:

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

PS H:\> $c.Matches

PS H:\>

管道$cGet-Member确认已返回MatchInfo对象:

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;}                                        

其他属性,例如Pattern和Line工作:

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不返回任何内容,$c本身为空:

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

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

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:\> 

我找到的解决方法(这里是SO)是使用.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)

由于此变通方法返回Select-String的预期匹配项,因此我可以继续编写脚本。

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

...
关于,
Schwert酒店

1 个答案:

答案 0 :(得分:1)

来自Get-Help Select-String -Parameter SimpleMatch

  

-SimpleMatch [<SwitchParameter>]

     

使用简单匹配而不是正则表达式匹配。在简单匹配中,Select-String在输入中搜索Pattern参数中的文本。 它不会将Pattern参数的值解释为正则表达式语句。

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