【问题标题】:RegEx not matching when using Select String使用选择字符串时正则表达式不匹配
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • “明天你可以接受自己的答案”我会尽快做的:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-30
  • 1970-01-01
  • 2012-06-05
  • 2013-12-25
相关资源
最近更新 更多