【发布时间】:2020-03-13 18:13:02
【问题描述】:
我有这段文字
[john]
age=20
group=wheel
[doe]
age=30
group=wheel
[stack]
age=undefined
group=wheel
color=white
[overflow]
age=undefined
我想用第一个正则表达式捕获:
age=20
和 group=wheel 的 [stack] 与第二个
【问题讨论】:
-
/\[john\].*?(age=\d+).*\[stack\].*?(group=[^\r\n]+)/gs会给你$1和$2你所寻找的 -
感谢您的回复,但是使用 ansible 我不能选择 $1 和 $2,它需要完全匹配,因此我需要 2 个完全匹配正则表达式的不同正则表达式。
-
Ansible 是否支持
\K? -
Ansible 使用 python 正则表达式 (docs.python.org/2/library/re.html)。我认为 python 没有 \K 但我不确定。
-
Python 确实不支持
\K。你运气不好。您不能使用捕获组,不支持\K,也不支持使用可变长度后视的遥远的第三个选项。没有办法干净地检索您想要的结果。实际上,age=20是可行的,但group=wheel不是。