正则表达式多重匹配

正则表达式多重匹配

本文介绍了Powershell - 正则表达式多重匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许我的推理有问题,但我无法使其正常工作.

这是我的正则表达式:(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;$匹配[0] }写主机 $dstate

输出:

设备#0 设备#1 设备#2 设备#3 设备#4

$matches[1] 的输出相同,$matches[2] 为空.

有没有办法获得所有匹配项,例如在 regex101.com 上?我正在尝试将输出/字符串拆分为单独的变量(一个用于 Device0,一个用于 Device1、Device2,等等).

更新: 这是命令行工具的输出:http://pastebin.com/BaywGtFE

解决方案

我在此处的字符串中使用了您的示例数据进行测试.这应该有效,尽管它可能取决于您的样本数据来自何处.

使用 powershell 3.0 我有以下内容

$getdevice |select-string -pattern '(?smi)(Device\s#\d+?(.*?)*?(?=Device\s#|\Z))' -AllMatches |ForEach-Object {$_.Matches} |ForEach-Object {$_.Value}

或者如果您的 PowerShell Verison 支持它...

($getdevice | select-string -pattern '(?smi)(Device\s#\d+?(.*?)*?(?=Device\s#|\Z))' -AllMatches).Matches.Value

返回 4 个带有设备 ID 的对象.我不知道您是否想要这些,但是如果您不需要这些,可以使用环视来修改正则表达式.如果发生这种情况,我更新了正则表达式以使用更多一位数字来说明设备 ID.

我使用的修饰符

  1. s 修饰符:单行.点匹配换行符
  2. m 修饰符:多行.导致 ^ 和 $ 匹配每一行的开始/结束(不是仅开始/结束字符串)
  3. i 修饰符:不敏感.不区分大小写的匹配(忽略 [a-zA-Z] 的大小写)

另一种以这种方式工作的正则表达式模式更短

'(?smi)(Device\s#).*?(?=Device\s#|\Z)'

Maybe my reasoning is faulty, but I can't get this working.

Here's my regex: (Device\s#\d(\n.*)*?(?=\n\s*Device\s#|\Z))

Try it: http://regex101.com/r/jQ6uC8/6

$getdevice is the input string. I'm getting this string from the Stream/Output from a command line tool.

$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

Output:

Same output for the $matches[1], $matches[2] is empty.

Is there a way I can get all matches, like on regex101.com? I'm trying to split the Output/String into separate variables (one for Device0, one for Device1, Device2, and so on).

Update: Here's the Output from the command line tool: http://pastebin.com/BaywGtFE

解决方案

I used your sample data in a here-string for my testing. This should work although it can depend on where your sample data comes from.

Using powershell 3.0 I have the following

$getdevice |
    select-string -pattern '(?smi)(Device\s#\d+?(.*?)*?(?=Device\s#|\Z))' -AllMatches |
    ForEach-Object {$_.Matches} |
    ForEach-Object {$_.Value}

or if your PowerShell Verison supports it...

($getdevice | select-string -pattern '(?smi)(Device\s#\d+?(.*?)*?(?=Device\s#|\Z))' -AllMatches).Matches.Value

Which returns 4 objects with their device id's. I don't know if you wanted those or not but the regex can be modified with lookarounds if you don't need those. I updated the regex to account for device id with more that one digit as well in case that happens.

The modifiers that I used

Another regex pattern thats works in this way that is shorter

'(?smi)(Device\s#).*?(?=Device\s#|\Z)'

这篇关于Powershell - 正则表达式多重匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:58