Notepad++ 在字符串/字符的每第 n 次出现处插入新行 [英] Notepad++ insert new line at every nth occurrence of a string/character
问题描述
使用 Notepad++ 查找和替换功能,我想在每个 n 出现的字符或字符串(在我的例子中是逗号)处插入一个新行.
Using Notepad++ Find and Replace feature, I would like to insert a new line at every nth occurrence of a character or string (a comma in my case).
我已经使用正则表达式"模式尝试了下面的正则表达式,但没有成功.
I have tried the regex below using "Regular expression" mode, but no luck.
查找内容: ((,){1000})
替换为:\1\n
推荐答案
如果您的意思是在一行中出现第 n 次任何字符串后添加换行符,我会使用
If you mean to add a newline after nth occurrence of any string on a line, I'd use
(?:.*?,){2}
并替换为 $&\n
(或 $&\r\n
),其中 .*?
匹配任何 0+换行符以外的字符,尽可能少,直到第一次出现 ,
.$&
是对整个匹配值的反向引用(2
用于演示看起来更干净,1000
是一个相当大的值).看一个演示,显示在每秒钟 ,
之后放置一个换行符.
and replace with $&\n
(or $&\r\n
) where .*?
matches any 0+ chars other than line break characters, as few as possible, up to the first occurrence of ,
. The $&
is the backreference to the whole match value (2
is used for the demo to look cleaner, 1000
is a rather big value). See a demo showing that a newline is placed after each second ,
.
对于单个字符,您最好使用否定字符类(但在那里添加换行符以强制模式不会跨行溢出):
With a single char, you'd better use a negated character class (but add line break chars there to force the pattern to not overflow across lines):
(?:[^\n\r,]*,){2}
这篇关于Notepad++ 在字符串/字符的每第 n 次出现处插入新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!