regex - 如何使用正则表达式捕获多个未知数量的值
问题描述
我想使用正则表达式捕获多个字符串。我希望 $matches 将同时包含 a digita
、digitb
和digitc
值。它似乎捕捉digita
并停止。如果可能的话,我希望捕获与订单无关。我怎样才能做到这一点?
PS C:\src\t> $s2 = 'a=3 c=5 b=4'
PS C:\src\t> $s2 -match 'a=(?<digita>[0-9])|b=(?<digitb>[0-9])c=(?<digitc>[0-9])'
True
PS C:\src\t> $matches
Name Value
---- -----
digita 3
0 a=3
解决方案
您可以扭转问题并执行以下操作:
,"a","b","c" | % { "$_=(?<digit$_>[0-9])" } | % { $s2 -match $_ } | % { $matches }
输出
Name Value
---- -----
digita 3
0 a=3
digitb 4
0 b=4
digitc 5
0 c=5
推荐阅读
- java - 在我的 jtable 中显示数千条 mysql 记录
- javascript - 如何根据多个对象属性有条件地删除数组中的对象?
- jenkins - 如何将加密密码从詹金斯传递到 gradle?
- c++ - 仅显示给定时间的输出
- git - git:合并拉取请求后如何获取新的开发分支?
- python - Python 3 按变量中指定的数量更新 sqlite 列
- pdf - iText 使用相同的代码生成不同的 pdf
- java - 如何总结地图中的双打
使用Java Stream分组后列表中的对象 - ios - 表达式类型 '(Bool, Error?) -> Void' 在没有更多上下文的情况下是模棱两可的
- python - 使用 Python Regex 提取代码的非注释部分