Ethelred The Unready
Ethelred The Unready

Reputation: 55

How to clear lines after the last regex match

I got an huge log of records I need to turn into a table. Each line has a record, preceded by date and time, something like this:

27/11/2019 16:35 - i don't need this  
28/11/2019 17:25 - don't need this either 
30/11/2019 11:33 - stuff i'm looking for
01/12/2019 08:11 - stuff that i'm also looking for 
03/11/2019 09:39 - don't need this

I want to completely clear the file from all the lines that I don't need. I'm able to clear most of the lines that I don't want if I use the following regex and substitution patterns (in notepad++, using the flag in which dot matches newline):

.+?(?<datetime>[\d\/]+\s[\d:]+)\s-\s(?<mystuff>stuff[^\n]+)

'${datetime};${mystuff}

However, I can't clear the lines after the last match. How could I do so?

Upvotes: 3

Views: 80

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626738

You may use

Find What:      ^(?:.+?([\d/]+\h[\d:]+)\h-\h(stuff.*)|.*\R?)
Replace With: (?{1}$1;$2)

Details

  • ^ - start of a line
  • (?:.+?([\d/]+\h[\d:]+)\h-\h(stuff.*)|.*\R?) - match either
    • .+? - any 1+ chars, as few as possible
    • ([\d/]+\h[\d:]+) - Group 1: one or more digits or /, a horizontal whitespace, one or more digits or :
    • \h-\h - a horizontal whitespace, - and a hor. whitespace
    • (stuff.*) - Group 2: stuff and the rest of the line
  • | - or
    • .* - any 0+ chars other than linebreak chars
    • \R? - an optional line break sequence.

The (?{1}$1;$2) replacement pattern only replaces with $1;$2 if Group 1 matches.

See the Notepad++ demo:

enter image description here

Upvotes: 3

Related Questions