regexreplacenotepad++text-processing

Add ) at End of Lines Containing ( but Not )


I need help with a regular expression in Notepad++. I want to:

  1. Find lines that contain a ( character but do not contain a ) character.
  2. Add a ) at the end of those lines.

I tried using this regex to find the lines:

^(?=.*\()(?!.*\)).*$

Then, I used this in the Replace with field to add ) at the end:

\0)

I also tried using $0) and other methods, but it either deletes the line or doesn’t work at all.

for example following is my sample list:

5years

Strta(Nicaragua)

3years

Ismtrtttu(Turkey)

2years

Frrtto(Spain

Lyears

Fulrttsta(Cuba)

0years

Chutrtrtl(UnitedKingdom)

Can someone help me find the correct regex and replacement pattern for this in Notepad++? Thank you!


Solution

  • You don't need any lookarounds, you can make use of a negated character class.

    Find what:

    ^[^\n()]*\(\K[^\n()]*$
    

    The regex matches:

    Replace with: the full match followed by a closing parenthesis:

    $0\)
    

    See a regex101 demo

    enter image description here