Tip - Find / Replace with RegEx Capture Groups - Jon Galloway

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:
<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:
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 ]

Published Saturday, May 24, 2003 12:45 AM by Jon Galloway
Filed under:


# re: Tip - Find / Replace with RegEx Capture Groups

Do you want to see good pics?,

Sunday, June 15, 2008 1:39 PM by Edtcgook

# re: Tip - Find / Replace with RegEx Capture Groups

Hi Jon,

Thanks for this!

Would you also know how to put the contents of the quoted string into the variable without the quotes?

Thanks, Wiebe

Saturday, December 5, 2009 1:04 PM by wtijsma

# re: Tip - Find / Replace with RegEx Capture Groups

I take pleasure in, result in I discovered exactly what I was having a look for.

You've ended my 4 day lengthy hunt! God Bless you man. Have a great day. Bye

Tuesday, August 21, 2012 4:19 AM by Ragan