Search code examples
regexstringnotepad2

Regex replace - match and empty all lines not containing a specific character


I can not use grep. In fact, I am in Notepad2. When I want to remove lines containing character "c", I am using the replace dialog (Ctrl+H):

Search string: ".*c.*"
Replace with: "" (nothing)

After that, I sort the lines and I get rid of the empty lines.

But now I need to empty all lines that actually do not contain character "c". Is it possible to do it in Notepad2?

If I can do it in Notepad2, then I can do it using JavaScript's String replace too, I guess.


Solution

  • Yes, you could anchor your pattern and use a negated character class.

    Find: ^[^c]*$
    

    Explanation:

    ^          # the beginning of the string
     [^c]*     # any character except: 'c' (0 or more times)
    $          # before an optional \n, and the end of the string