Tip - Find / Replace with RegEx Capture Groups

Find / replace with regular expression capture groups can be very powerful.

I needed to do a find replace that reused a portion of the find string in the replace - more clear in the following example:
Find:
<A target="_blank" HREF="(any link)">
Replace with:
<A target="_blank" HREF="(link found above)"><input type="checkbox" value="(link found above)" name="LinkList">

This isn't a simple find replace scenario, since I needed to reference the link twice in the replacement string. I wrote a macro to do this, but there were hundreds of occurrences and the macro was very slow. It turns out that the regex find / replace in VS.NET is perfect for this

Find criteria:
HREF={:q}\>
Replace string:
HREF=\1\>\<input type=checkbox value=\1 name=killList\>

The curly braces around the :q tells it to take the quoted string (:q) and put it in a variable. Then you can refer to that variable in the find or replace string as \1. If you use more than one {} group, it will go into \2, \3, etc.

[Listening to: La fredda lama del coltello - Ennio Morricone ]

2 Comments

Comments have been disabled for this content.