Regex: Delete lines that match the conditions

I often use it for log analysis. If you search and bulk replace using any regular expression from the table below, all unnecessary lines will be deleted.

Summary Regex
Line with only line breaks and whitespace characters ^\s*?$(\r\n|\r|\n)?
Line with only line breaks ^$(\r\n|\r|\n)?
Containing keyword ^.*keyword.*$(\r\n|\r|\n)?
Containing keywords ^.*(keyword1|keyword2).*$(\r\n|\r|\n)?
Start with keyword ^keyword.*$(\r\n|\r|\n)?
End with keyword ^.*keyword$(\r\n|\r|\n)?
Not containing keyword ^(?!.*keyword).*$(\r\n|\r|\n)?
Not containing keywords ^(?!.*(keyword1|keyword2)).*$(\r\n|\r|\n)?
Not start with keyword ^(?!keyword).*$(\r\n|\r|\n)?
Not end with keyword ^(?!.*keyword$).*$(\r\n|\r|\n)?

It covers all line feed codes (CRLF, CR, LF).

How to use

  1. Open the file with a text editor that can use regex.
  2. Set the regex of the table in the search string.
  3. Set the empty string in the replacement string.
  4. Execute “Replace All”.

For example, suppose you have the following logs.

INFO  2018-01-31 15:00:00.000 1234/process1 message
ERROR 2018-01-31 15:00:00.000 1234/process1 message
INFO  2018-01-31 15:00:00.000 4321/process2 message
INFO  2018-01-31 15:00:00.000 1234/process1 message
INFO  2018-01-31 15:00:00.000 4321/process2 message
INFO  2018-01-31 15:00:00.000 1234/process1 message
INFO  2018-01-31 15:00:00.000 4321/process2 message
INFO  2018-01-31 15:00:00.000 4321/process3 message
INFO  2018-01-31 15:00:00.000 4321/process3 message
INFO  2018-01-31 15:00:00.000 1234/process1 message

If you want to see only the process1, the process2 and process3 are annoying. Resolve using the regex for “キーワードを含まない行” in the table.

^(?!.*process1).*$(\r\n|\r|\n)?

All replace using this regular expression.

INFO  2018-01-31 15:00:00.000 1234/process1 message
ERROR 2018-01-31 15:00:00.000 1234/process1 message
INFO  2018-01-31 15:00:00.000 1234/process1 message
INFO  2018-01-31 15:00:00.000 1234/process1 message
INFO  2018-01-31 15:00:00.000 1234/process1 message

Logs other than process1 have disappeared. Congratulations.

Here for a description of metacharacters.
Regex: Metacharacters

I tried to make it a tool.
Remove Specific Lines From a Text

Hirota Yano / Programmer
Born in 1988, based in Japan.
© yanohirota.com