【发布时间】:2016-05-17 16:51:02
【问题描述】:
我已经用这段代码验证了我的正则表达式是正确的:
#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 时正则表达式不匹配?
【问题讨论】:
-
提示:通过管道输出到 Get-Member
-
哦,我需要访问 $results.Matches。谢谢! :)
-
将 Matches 属性通过管道传递给 Get-Member,您应该在兔子洞中足够远了 :-)
标签: regex powershell select-string