2

我已经用这段代码验证了我的正则表达式是正确的:

#this is the string where I'm trying to extract everything within the []
$text = "MS14-012[2925418],MS14-029[2953522;2961851]"
$text -match "\[(.*?)\]"
$matches[1]

输出:

True
2925418

我想使用 Select-String 来获得我的结果,例如:

$result = $text| Select-String -Pattern $regex

输出:

MS14-012[2925418],MS14-029[2953522;2961851]

我还尝试了什么:

$result = Select-String -Pattern $regex -InputObject $text
$result = Select-String -Pattern ([regex]::Escape("\[(.*?)\]")) -InputObject $text

还有一些更多的变体以及不同种类的“和'围绕正则表达式等等。我真的没有想法......

谁能告诉我为什么当我使用 Select-String 时正则表达式不匹配?

4

1 回答 1

1

在将输出传送到 Get-Member 后,我注意到 Select-String 返回了一个 MatchInfo 对象,并且我需要访问 MatchInfo.Matches 属性才能获得结果。感谢 Mathias R. Jessen 给我的提示!;)

于 2016-05-17T17:08:06.417 回答