Reputation: 15372
I have a text document in Notepad++ with information separated by line, but want to delete everything but every seventh line. This line is always matched by the pattern (\d{4} :.*?\r\n)
.
How can I delete everything that does not match this pattern so that I just get every seventh line separated by \r\n
?
Upvotes: 1
Views: 183
Reputation: 14037
Open the search dialogue and select the Mark tab. In the Find what field enter a search string to find the lines to be kept. Make sure that Bookmark line and Regular expression are selected, then click Mark all. Next visit the menu => Search => Bookmark => Remove unmarked lines.
The question says the lines to be retained match (\d{4} :.*?\r\n)
. The capture brackets (
and )
are not needed as the capture is not used. Searches for \r\n
may often be rewritten as searching for $
, ie an end-of-line. Your search pattern is just looking for the first end-of-line after the earlier items. The search may be reduced to \d{4} :
.
Upvotes: 1
Reputation: 71538
You could maybe try:
^(?!\d{4} :)[\s\S]*?(?=\r\n\d{4} :)
[Note, I couldn't put \r
in there because I couldn't insert carriage returns in the input box somehow...]
^
is a beginning of line anchor and matches the beginning of a line.
(?!\d{4} :)
is a negative lookahead and will make the whole regex match only if there's no \d{4} :
at the beginning of the line (the position being indicated by ^
).
[\s\S]*?
is a character class that will match any and all character. The quantifier is a lazy quantifier that will cause matching to stop as soon as possible (this is determined by what's following)
(?=\r\n\d{4} :)
is a positive lookahead, and matches only when there's a \r\n\d{4} :
ahead.
If I understood your question well, this would be what you're looking for. All lines except the 7th lines get deleted and there's only one empty line left behind between each of those 7th line.
Upvotes: 3