How to use Negative Lookahead in Regex to Delete unwanted Lines

Atif Mohammed Ameenuddin picture Atif Mohammed Ameenuddin · Mar 9, 2013 · Viewed 13.2k times · Source

I need help regarding using negative lookahead. I am using Notepad++ and I want to delete all lines except the lines that contain <title>(.*)</title>

I tried a couple of things but that didnt work.

^.*(?!<title>).*</title>
^.*(?!<title>.*</title>)

Answer

nhahtdh picture nhahtdh · Mar 9, 2013

You are close:

^(?!.*<title>.*</title>).*

By this regex ^.*(?!<title>.*</title>), the regex engine will just find some position that it cannot find <title>.*</title> (end of line is one such valid position).

You need to make sure that, from the start of the line, there is no way you can find <title>.*</title> anywhere in the line. That is what my regex does.