我在新行上有文字,如下:
tom
tim
john
will
tod
hello
test
ttt
three
Run Code Online (Sandbox Code Playgroud)
我想删除每个第三行,所以使用上面的例子我要删除: john,hello,three
我知道这需要一些正则表达式,但我不是最好的!
我尝试了什么:
Search: ([^\n]*\n?){3} //3 in my head to remove every third
Replace: $1
Run Code Online (Sandbox Code Playgroud)
我试过的其他人只是尝试\n\r等等.再次,不是最好的正则表达式.我认为上述尝试有点接近.
这将删除每三行可能包含多个单词的行。
(?:[^\r\n]+\R){2}\K[^\r\n]+(?:\R|\z)LEAVE EMPTY解释:
(?: # start non capture group
[^\r\n]+ # 1 or more non linebreak
\R # any kind of linebreak (i.e. \r, \n, \r\n)
){2} # end group, appears twice (i.e. 2 lines)
\K # forget all we have seen until this position
[^\r\n]+ # 1 or more non linebreak
(?: # start non capture group
\R # any kind of linebreak (i.e. \r, \n, \r\n)
| # OR
\z # end of file
) #end group
Run Code Online (Sandbox Code Playgroud)
给定示例的结果:
tom
tim
will
tod
test
ttt
Run Code Online (Sandbox Code Playgroud)
屏幕截图: