【问题标题】:regex find and replace every nth occurrence in notepad++ [duplicate]正则表达式在记事本++中查找并替换每第n次出现[重复]
【发布时间】:2019-07-23 03:16:28
【问题描述】:

我有一个大文本文件(15000 行)。每行以 /txt 结尾 我想每 5 行更改一次以 txt5 结尾。 我搜索了互联网,但没有找到适合我的解决方案。

somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt

到:

somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt5
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt
somethingDifferent/txt5

【问题讨论】:

  • @WiktorStribiżew:这并不完全是重复的,因为它不处理源文件中的换行符。诚然,这是一个相当微不足道的问题。

标签: javascript regex notepad++


【解决方案1】:

用 EditPadPro 测试过,但这也应该在 Notepad++ 中工作:

搜索

((?:^.*/txt\r\n){4}^.*/txt$)

替换为

\15

说明:

(           # Start capturing group no. 1, referenced by \1 in the replacement expression
 (?:        # Start a non-capturing group that matches...
  ^.*/txt   # a complete line ending in /txt
  \r\n      # and a (Windows-style) carriage return
 ){4}       # four times in a row.
 ^.*/txt$   # Then match a fifth line just like that, but without the CR
)           # End of capturing group 

【讨论】:

  • 很好的答案,您能否解释一下 \1 的替换方式?
猜你喜欢
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 2023-01-12
  • 1970-01-01
相关资源
最近更新 更多