也许我的推理有问题,但我无法让它发挥作用。
这是我的正则表达式:(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