Categorized | Programming

Using Regular Expressions, Captured Groups in Notepad++ Find and Replace

Posted on 05 August 2010 by Jason Grimme

The past few days I have been doing research on …something that interests me very much, which I will save for another post. I have been entering this data into Excel, and today came upon a situation where doing some regex find / replace would be very helpful. I have used regular expressions in Notepad++ before, but I hadn’t done it with captured groups.
What I had was HTML code for a select box full of several colors. I wanted a quick way to extract the colors and get rid of the rest.

Notepad++ Find Replace with RegEx

The HTML

1
2
3
4
5
6
7
<select>
<option value="0" title="White" style="color: rgb(0, 102, 153);">White</option>
<option value="1" title="Cobalt Blue" style="color: rgb(0, 102, 153);">Cobalt Blue</option>
<option value="2" title="Almond" style="color: rgb(0, 102, 153);">Almond</option>
<option value="3" title="Empire Red" style="color: rgb(0, 102, 153);">Empire Red</option>
[....]
</select>

The Regular Expression

Regular expressions are easy, but I wanted to have a heading for the next section so I felt like this needed one as well. Rather than extracting the text from the actual label of the select option, I am grabbing the contents of the title attribute. Captured groups are created by surrounding with parenthesis.

1
.*title="([\w\s]+).*

Captured Groups

In Dreamweaver I remember referencing captured groups in find and replace with a dollar sign and then the number. This did not work in notepad++. Instead, I figured out you use a forward slash (/). So, for the first captured group, you do \1. For the second, \2, etc.

1
\1

Make sure that you check the “Use Regular Expressions” checkbox, otherwise NotePad++ will think you are crazy.

Tags | , , ,

Leave a Reply

Security Code:

-->