【发布时间】:2014-09-10 04:55:53
【问题描述】:
也许我的推理有问题,但我无法让它发挥作用。
这是我的正则表达式:(Device\s#\d(\n.*)*?(?=\n\s*Device\s#|\Z))
试试看:http://regex101.com/r/jQ6uC8/6
$getdevice 是输入字符串。我从命令行工具的流/输出中获取此字符串。
$dstate = $getdevice |
select-string -pattern '(Device\s#\d(\n.*)*?(?=\n\s*SSD\s+|\Z))' -AllMatches |
% { $_ -match '(Device\s#\d(\n.*)*?(?=\n\s*SSD\s+|\Z))' > $null; $matches[0] }
Write-Host $dstate
输出:
设备#0 设备#1 设备#2 设备#3 设备#4
$matches[1] 的输出相同,$matches[2] 为空。
有没有办法让我获得所有匹配项,例如在 regex101.com 上?我正在尝试将输出/字符串拆分为单独的变量(一个用于 Device0,一个用于 Device1、Device2 等)。
更新:以下是命令行工具的输出:http://pastebin.com/BaywGtFE
【问题讨论】:
-
欢迎来到 Stack Overflow,@Frankstar。请在问题中直接提供尽可能多的信息。我从 regex101 得到了正则表达式并把它放在这里。 (不过,regex101 的链接很棒!)。
-
你好@alliteralmind !我尽力了 - 谢谢你的更新。在以后的问题中会记住这一点。
标签: regex powershell powershell-4.0 regex-group