正则表达式替换:仅替换 "or" 条件中的第一次出现
Regex Substitution: only replace the first occurance from a "or" condition
我需要将\d+
和a+
替换为x
,我想将所有\d+
替换为x
,但是对于a+
, 如果有的话,我只想替换第一次出现的。
是否可以只使用一个正则表达式来实现这一点?
Input
Desired Output
123,456,789
x,x,x
123,456,a,789
x,x,x,x
aaa,aa,aaa
x,aa,aaa
aa,123,456,aaaa,789
x,x,x,aaaa,x
aaa,123,456,aa,789,aaaa
x,x,x,aa,x,aaaa
This 肯定不行:
/\d+|a+/gm
我正在使用 PCRE 正则表达式引擎,或者是否有任何模式修饰符或控制动词可以做到这一点?提前致谢。
经过认真的斗争,我想出了这个主意:
(?:(\G|^)|)(?(1)(?:a+|\d+([^a\d]+))|\d+)
替换为
x
(?:(\G|^)|)
Testing last anchor \G
or the start of the string ^
before every match optionally, and put the caught anchor in group 1
(?(1)(?:a+|\d+([^a\d]+))|\d+)
- If cause, if the anchor has been detected, matching
a+
or \d+
- If the sequence detected is
\d+
, also matches the ([^a\d]+)
to keep the anchor for next match.
- If the sequence detected is
a+
, only matches a+
, so the anchor will be broken for the next test.
- While the anchor
\G
is not detected in group 1, that means a+
has already been detected, after that only matches \d+
, and the \G
will never be detected anymore.
这是proof
好像只在这个场景有用,不知道对真题有没有用。
抱歉,我忘记了答案必须 post 放在一边,post 首先在评论中。
现在是:
正则表达式:\d+|((?<!a|(?!(?1)).))a+
替换:x
我需要将\d+
和a+
替换为x
,我想将所有\d+
替换为x
,但是对于a+
, 如果有的话,我只想替换第一次出现的。
是否可以只使用一个正则表达式来实现这一点?
Input | Desired Output |
---|---|
123,456,789 |
x,x,x |
123,456,a,789 |
x,x,x,x |
aaa,aa,aaa |
x,aa,aaa |
aa,123,456,aaaa,789 |
x,x,x,aaaa,x |
aaa,123,456,aa,789,aaaa |
x,x,x,aa,x,aaaa |
This 肯定不行:
/\d+|a+/gm
我正在使用 PCRE 正则表达式引擎,或者是否有任何模式修饰符或控制动词可以做到这一点?提前致谢。
经过认真的斗争,我想出了这个主意:
(?:(\G|^)|)(?(1)(?:a+|\d+([^a\d]+))|\d+)
替换为
x
(?:(\G|^)|)
Testing last anchor
\G
or the start of the string^
before every match optionally, and put the caught anchor in group 1
(?(1)(?:a+|\d+([^a\d]+))|\d+)
- If cause, if the anchor has been detected, matching
a+
or\d+
- If the sequence detected is
\d+
, also matches the([^a\d]+)
to keep the anchor for next match.- If the sequence detected is
a+
, only matchesa+
, so the anchor will be broken for the next test.- While the anchor
\G
is not detected in group 1, that meansa+
has already been detected, after that only matches\d+
, and the\G
will never be detected anymore.
这是proof
好像只在这个场景有用,不知道对真题有没有用。
抱歉,我忘记了答案必须 post 放在一边,post 首先在评论中。
现在是:
正则表达式:\d+|((?<!a|(?!(?1)).))a+
替换:x