Reputation: 31
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
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
Reputation: 91430
(?:^.*?-.*(?:\R|\z)){4,}
LEAVE EMPTY
. matches newline
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):
Screenshot (after):
Bookmark lines:
Upvotes: 4