darjuha
darjuha

Reputation: 31

Regex to delete consecutive lines with a specific symbol

I have a huge text file to sort and I noticed that the lines I don't need usually come with a hyphen - symbol and more than 3 lines in a row. So I want to use regex to remove these lines.

I tried this: ^.*(?:\-.*?){3}.*(?:\R|\Z) but it works only within a single line while I need to remove only consecutive lines with - starting from 3 and more.

Example of my text:

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

desired output

Good Line 1
Good Line 2
Good Line 3

Upvotes: 2

Views: 1003

Answers (2)

Haji Rahmatullah
Haji Rahmatullah

Reputation: 430

I would use ...
Find what: ^(?:.*Error-).*\s?
Replace with: nothing
This will find any line that have Error- and remove it....

Explanation

  ^                   # 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)  

Upvotes: 0

Toto
Toto

Reputation: 91430

  • Ctrl+H
  • Find what: (?:^.*?-.*(?:\R|\z)){4,}
  • Replace with: LEAVE EMPTY
  • CHECK Wrap around
  • CHECK Regular expression
  • UNCHECK . matches newline
  • Replace all

Explanation:

(?:                 # 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

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here


Bookmark lines:

enter image description here

Upvotes: 4

Related Questions