Understanding regular expressions with Notepad++

In this post, I would like to emphasize on the power of Notepad++ with regular expressions. It’s not just a text editor or any medium to check your opening closing tags and braces. With thousands of line to edit as a result of some requirement change, involving hundreds of files within a directory, how many hours do you think you can finish off in? I guarantee it would take around 6-7 hours with 99% perfection and further an hour or so, to find this 0.1% mistake you did while editing.

As a purpose to demonstrate, I created a simple text file, demo.txt as shown below:

Notepad++ Tricks


Scenario 1:

Method 1:

I need to remove the text that follows the word ‘demo

Select ‘demo’ in the notepad++ window and press Ctrl+H

Select the search mode as ‘Regular Expression

Find for (\<demo\>)(.*$)

() – Used for grouping during Find and Replace

So I formed two groups: (\<demo\>) and (.*$)

\1 corresponds to (\<demo\>)

\2 corresponds to (.*$)

Click on ‘Find Next‘, you will see something like:

Notepad++

 

In the ‘Replace with‘ box, just enter \1

So, ‘demo‘ word followed by anything on a line, gets replaced now with ‘demo‘ as:

Notepad++

Method 2:

Find what: (demo)[ ].*$

Replace with: demo

Scenario 2:

Add something to the start of every line

Find what: ^

Replace with: <some-text>

Scenario 3:

Delete all empty lines

Find what: \r

Replace with: <blank>

Scenario 4:

Add something to the end of every line

Find what: \$

Replace with: <blank>

Queries most welcomed as comments to this post.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.