regex - 使用“选择字符串”时,RegEx不匹配

标签 regex powershell 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返回了MatchInfo对象,并且我需要访问MatchInfo.Matches属性来获取结果。感谢Mathias R. Jessen给我提示! ;)

关于regex - 使用“选择字符串”时,RegEx不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37282144/

相关文章:

C++ Clang 无法解析此 Lookbehind 正则表达式

Javascript 替换问题(使用 alt 和 title 属性)

javascript - IE8 中的意外量词

powershell - Select-String 输出给出整个对象并且不响应过滤器

powershell - 在文本文件中搜索字符串,但得到多个结果

javascript - RegEx ip/mask(例如:192.168.1.1/24)

azure - 在模板中使用 powershell 脚本

arrays - 在 PowerShell 中通过引用传递/更新哈希表和数组

c# - 尝试在 C# 中运行 powershell 脚本

powershell - PowerShell 中的着色数组筛选器