我已验证此代码的正则表达式正确:
#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返回了MatchInfo对象,并且我需要访问MatchInfo.Matches属性来获取结果。感谢Mathias R. Jessen给我提示! ;)
关于regex - 使用“选择字符串”时,RegEx不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37282144/