Regular expressions in notepad++

One of the first tools I always download when I’m face with a new Windows computer is notepad++. This clean and simple, yet very powerful, text editor is in my opinion an indispensible tool when working on a windows computer.

Except for the very useful “Find in Files” feature, which I’ve found to be light-years better than Windows’ built-in search feature, the regular expressions-type search (and replace) feature is also very useful.

Here’s an example on how to use this powerful feature, as I always tend to forget the exact syntax:

If I want to replace each line starting with either “#” or “;” with an empty line, or to completely remove the line, here’s how to do it:

Find:
^[#;].*

Replace:
(leave this emtpy.)

The ^ indicates the start of a line, the [#;] is a character class to match either # or ;, and .* matches anything else in the line.

In versions of Notepad++ before 6.0, you won’t be able to actually remove the lines due to a limitation in its regex engine; the replacement results in blank lines for each line matched. In other words, this:

# foo
; bar
statement;

Will turn into:

statement;

However, the replacement will work in Notepad++ 6.0 if you add \r, \n or \r\n to the end of the pattern, depending on which line ending your file is using, resulting in:
statement;

Be sure to select “Search Mode” -> “Regular Expression”

For example ^[#;].*\r\n in “Find what” and “” in Replace with will remove the entire line if it starts with # or ;!!

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *