Search code examples
regexnotepad++

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

Solution

    • 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