regex - 替换正则表达式循环中的组
问题描述
我有这两条线:
What is P(output1|cause1=2, cause2=2)
What is P(output2|cause3=2)
我想将其更改为:
method_to_use(model, {"cause1": 2, "cause2": 2}, "output1")
method_to_use(model, {"cause3": 2}, "output2")
这是我的正则表达式:
.*P[(]([a-z1-9]+)[|](([a-z1-9]+)=([1-9]),?)+[)]
我尝试像这样替换它:
method_to_use(model, {"$3": $4}, "$1")
但我只得到该组的最后一个:
method_to_use(model, {"cause2": 2}, "output1")
是否可以做某种“循环”并在途中改变所有配合?
解决方案
您可以将字符串与以下正则表达式匹配。
^.*P\(([^|]+)\|([^=]+)=(\d+)(?:, +([^=]+)=(\d+))?\)$
如果捕获组 4 非空,则将(整个字符串)匹配替换为
method_to_use(model, {"$2": $3, "$4": $5}, "$1")
这会导致字符串
What is P(output1|cause1=2, cause2=2)
替换为
method_to_use(model, {"cause1": 2, "cause2": 2}, "output1")
如果捕获组 4 为空,则将匹配替换为
method_to_use(model, {"$2": $3}, "$1")
这会导致字符串
What is P(output2|cause3=2)
替换为
method_to_use(model, {"cause3": 2}, "output2")
请注意,两个链接处的正则表达式是等价的,唯一的区别是在 Demo 1 中,我以自由间距模式表达了正则表达式,这允许它是自文档化的。
除了替换整个字符串之外,当然可以简单地从捕获组的值中形成新字符串。如果这是^.*P
在正则表达式的开头完成,则可以将其更改为 simple P
。
正则表达式引擎执行以下操作。
^ # match beginning of line
.*P\( # match 0+ chars then '|('
([^|]+) # save 1+ chars except '|' in cap grp 1 (output)
\| # match ':'
([^=]+) # save 1+ chars except '=' in cap grp 2 (causeA)
= # match '='
(\d+) # save 1+ digits in cap grp 3 (causeAval)
(?: # begin non-cap grp
,\ + # match ',' then 1+ spaces
([^=]+) # match 1+ chars except '=' in cap grp 4 (causeB)
= # match '='
(\d+) # match 1+ digits in cap grp 5 (causeBval)
)? # end non-cap grp and make it optional
\) # match ')'
$ # match end of line
推荐阅读
- domain-driven-design - 同一聚合中的依赖实体
- hash - 在合并语句中使用哈希键作为比较列
- javascript - 使用 WebUSB 读取 K3 / K3-Pro 温度计
- odoo - odoo13 归档此规则“res.partner.rule.private.employee”时创建客户的问题
- javascript - 为什么 127.0.0.1/routeToFile/index.html 不起作用?
- database - 一系列短交易造成的饥饿
- rust - 在 toml 中导入带有别名的 rust 包
- swagger - Swagger/OpenAPI $ref 包含在描述标签中
- node.js - 错误代码 429 请求过多,但我仍有积分
- c# - 如何使用实体框架从今天开始选择最接近的日期?