Search code examples
regexnotepad++

notepad++ how to keep all the line start with # and delete the rest of the line


I kinda know how to keep delete all line start with # with the code

^[#].*

But I wonder how to do the reverse? keep all the line start with # and delete the rest.


Solution

  • In order to do that, you just use negated character class in your regex:

    ^[^#].+
    

    [^#] means match everything except #, so please see picture below for better understanding: enter image description here

    Then just replace all, this will empty lines. So then you need to match new line characters only with ^\r\n (depending on operation system, it could be also ^\n) and again replace it with empty string: enter image description here