正则表达式删除具有特定符号的连续行

Regex to delete consecutive lines with a specific symbol

我有一个巨大的文本文件要排序,我注意到我不需要的行通常带有连字符 - 符号并且连续超过 3 行。所以我想使用正则表达式来删除这些行。

我试过这个:^.*(?:\-.*?){3}.*(?:\R|\Z) 但它只能在一行内工作,而我只需要删除连续的行 - 从 3 开始。

我的文字示例:

Good Line 1
Error-1
Error-2:3045
Error-3-32
Good Line 2
Error-4_sub
Error-5.0
Error-6...0
Error-7
Error-8-9
Error-9
Good Line 3

期望的输出

Good Line 1
Good Line 2
Good Line 3
  • Ctrl+H
  • 查找内容:(?:^.*?-.*(?:\R|\z)){4,}
  • 替换为:LEAVE EMPTY
  • 检查 环绕
  • CHECK 正则表达式
  • 取消选中 . matches newline
  • 全部替换

解释:

(?:                 # Beginning non capture group
^                   # beginning of line
  .*?               # 0 or more any character but newline, not greedy
  -                 # hyphen
  .*                # 0 or more any character but newline
  (?:\R|\z)         # non capture group, any kind of linebreak OR end of file
){4,}               # end group, must appear 4 or more times
                        # set the value to your needs

截图(之前):

截图(后):


书签行:

我会用...
查找内容:^(?:.*Error-).*\s?
替换为:nothing
这将找到任何包含 Error- 的行并将其删除....

说明

  ^                   # beginning of line
  (?:                 # Beginning non capture group  
  .*               # 0 or more any character but newline,   
  Error-           # literally  Error plus hyphen   
  )                    # closing of non captured group   
  .*                # 0 or more any character but newline  
  \s              # matches any whitespace character (equivalent to [\r\n\t\f\v ])  
   ?               # matches the previous token between zero and one times, as many times as possible, giving back as needed (greedy)