简体   繁体   English

如何在Notepad ++中用正则表达式替换子字符串的所有奇数出现?

[英]How do I replace every odd occurrence of a substring with regex in Notepad++?

I would like to change the following 我想更改以下内容

<evenInstance><evenInstance><evenInstance><evenInstance><evenInstance>

to

<oddInstance><evenInstance><oddInstance><evenInstance><oddInstance>

I have gotten the following 我得到了以下

<oddInstance><oddInstance><oddInstance><oddInstance><oddInstance><oddInstance>

to be 成为

<oddInstance><evenInstance><oddInstance><evenInstance><oddInstance><evenInstance>

with the regex: 与正则表达式:

(odd(?:(?!odd).)*)odd((?:(?!odd).)*)

and the substitution: 和替代:

\1even\2

But I don't know how to do it for odd occurrences. 但是我不知道怎么处理。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

The first pattern might be shortened to 第一种模式可能会缩短为

odd\Kodd(?=(?:oddodd)*$)

Replace with even 替换为even

Regex demo 正则表达式演示

Result 结果

oddevenoddevenoddeven 奇数

For the second pattern you might use: 对于第二种模式,您可以使用:

even(?=(?:eveneven)*$)

Replace with odd 替换为odd

Regex demo 正则表达式演示

Result 结果

oddevenoddevenodd 古怪的

This is the fastest way to do it. 这是最快的方法。
Uses the \\G construct. 使用\\G构造。

(?s)(?:even|(?!^)\\G.*?even.*?\\Keven)

https://regex101.com/r/8BlRJx/1 https://regex101.com/r/8BlRJx/1

Formatted 格式化的

 (?s)
 (?:
      even
   |  
      (?! ^ )
      \G 
      .*? even .*? \K even
 )

Benchmark 基准测试

Regex1:   (?:even|(?!^)\G.*?even.*?\Keven)
Completed iterations:   3  /  3     ( x 1000 )
Matches found per iteration:   186
Elapsed Time:    1.02 s,   1023.95 ms,   1023953 µs
Matches per sec:   544,946

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM