将-SimpleMatch与Select-String一起使用时未捕获到的匹配项 [英] Matches not captured when using -SimpleMatch with Select-String
问题描述
我一直在使用正则表达式来解析某些XML节点内的文本.但是,当我将-SimpleMatch
与Select-String
一起使用时,MatchInfo对象似乎不包含任何匹配项.
I have been using regex to parse text within some XML nodes. However, when I use -SimpleMatch
with Select-String
, the MatchInfo object doesn't appear to contain any matches.
我在网上找不到任何表明此行为正常的信息.我现在想知道这是否是我的Powershell安装. (供参考:我使用的计算机已安装Powershell 3.0.)
I am unable to find anything online that indicates this behavior is normal. I am now wondering if it is my Powershell installation. (For reference: the computer I'm using has Powershell 3.0 installed.)
使用一个非常简单的示例,使用正则表达式模式时,我们可以返回预期的MatchInfo对象:
Using a very simple example, we can return the expected MatchInfo Object when using the regex patterning:
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属性:
But adding the -SimpleMatch
parameter appears to not return the Matches property in the MatchInfo object:
PS H:\> $c = "abd 14e 568" | Select-String -Pattern "ab" -SimpleMatch
PS H:\> $c.Matches
PS H:\>
将$c
输给Get-Member
确认返回了MatchInfo对象:
Piping $c
to Get-Member
confirms a MatchInfo Object was 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;}
以及其他属性(例如图案和线条):
And other properties, such as Pattern and Line work:
PS H:\> $c.Pattern
ab
PS H:\> $c.Line
abd 14e 568
此外,将索引值发送到Matches数组时不会产生错误:
Also, no error is generated when sending an index value to the Matches array:
PS H:\> $c.Matches[0]
PS H:\>
我不确定如何解释结果,也不确定为什么会这样.
I am not certain how to interpret the results, nor am I certain why it is occuring.
这种行为是有问题的,因为很多时候我必须搜索包含正则表达式特殊字符的字符串,()很常见.
This behavior is problematic, as there are many times I must search for strings that contain regex special characters, ()'s are very common.
扩展示例:
PS H:\> $c = "ab(d) 14e 568" | Select-String -Pattern "ab(d)"
PS H:\> $c.Matches
PS H:\>
$c.Matches
不返回任何内容,并且$c
本身为null,这是由于在正则表达式模式中使用了括号:
$c.Matches
returns nothing, and $c
itself is null, due to the use of the parentheses in the regex pattern:
PS H:\> $c -eq $null
True
使用-SimpleMatch
确实会产生MatchInfo对象,但仍不返回任何匹配项:
Using -SimpleMatch
does produce a MatchInfo object, but still does not return any matches:
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选择字符串由于转义序列而失败 )
The work-around I found (here on SO) is to use the Regex.Escape method from .NET:
(reference: 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
返回了预期的匹配项,因此我能够继续执行脚本.
As this workaround returns the expected matches from Select-String
, I have been able to continue my scripting.
但是我很好奇为什么使用-SimpleMatch
参数时没有返回任何匹配项.
But I am curious as to why no matches are returned when using the -SimpleMatch
parameter.
...
问候,
施韦特
...
With regards,
Schwert
推荐答案
来自Get-Help Select-String -Parameter SimpleMatch
:
-SimpleMatch [
<SwitchParameter>
]
使用简单匹配而不是正则表达式匹配.通过简单匹配,Select-String在输入中搜索Pattern参数中的文本. 它不会将Pattern参数的值解释为正则表达式语句.
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
进行子字符串搜索.它会返回一个MatchInfo
对象,其中包含字符串和相关的上下文信息(如果存在),但不会返回Matches
,因为从未对字符串进行过正确的正则表达式匹配-就这么简单
So, SimpleMatch
simply does a substring search for $Pattern
inside each string you pipe to it. It returns a MatchInfo
object containing the string and relevant contextual information if present, but no Matches
since a proper regex matching was never carried out against the string - it's as simple as that
这篇关于将-SimpleMatch与Select-String一起使用时未捕获到的匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!