Search code examples
regexnotepad++

regex negative lookahead how to find a string that ends with a series of digits and a parenthesis?


I have this regex for Notepad++ which works to find the strings I am supposed to find which are all lines ending each with a number in brackets. ^.*\(\d*\)\r\n Problem is: Those are the lines I want to keep with a regex find & replace where I erase the other lines.

I tried

^.*?(?!\(\d*\))\r\n
^.*?(?!(\(\d*\)))\r\n
(?!(^.*?\(\d*\)\r\n))

None of these seem to work (or not work correctly).

What am I doing wrong?


Solution

  • I think this regex should only capture lines that does not end with numbers with parenthesis at the end:

    ^(?!.*\(\d+\)$).*$
    

    Demo

    • ^ asserts position at start of a line
    • (?!.*\(\d+\)$) negative look ahead to see if the line not ends with number inside parenthesis.
    • .* mathces the full line
    • $ asserts position at the end of a line