使用 Select 字符串时 RegEx 不匹配

RegEx not matching when using Select String

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

#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 后,我注意到 Select-String returns 是一个 MatchInfo 对象,我需要访问 MatchInfo.Matches 属性 才能获取结果。感谢 Mathias R. Jessen 给我提示! ;)