簡體   English   中英

如何在Notepad ++中用正則表達式替換子字符串的所有奇數出現?

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

我想更改以下內容

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

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

我得到了以下

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

成為

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

與正則表達式:

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

和替代:

\1even\2

但是我不知道怎么處理。 任何幫助是極大的贊賞。

第一種模式可能會縮短為

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

替換為even

正則表達式演示

結果

奇數

對於第二種模式,您可以使用:

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

替換為odd

正則表達式演示

結果

古怪的

這是最快的方法。
使用\\G構造。

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

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

格式化的

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

基准測試

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