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

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

Comments

# Visual Studio Find (and Replace) Regular Expressions | Aaron Lerch

Pingback from  Visual Studio Find (and Replace) Regular Expressions | Aaron Lerch

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

Do you want to see good pics?,

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

# Tip - Find / Replace with RegEx Capture Groups in Visual Studio

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Monday, July 21, 2008 8:52 AM by DotNetKicks.com

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

I bookmarked this guestbook.,

Wednesday, July 29, 2009 3:36 PM by name

# note-在VisualStudio中使用正则表达式

前言:本来昨天已经写了,但由于意外给搞丢失了,由于刚刚看了这篇文章知道了一些真相;现在的心理状态已经和昨天不一样了,昨天是满心的高兴,对VisualSduio很有好感,当时自认为是没有把正则学好,所以...

Tuesday, August 04, 2009 11:27 PM by qinghao

# 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 05, 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

Leave a Comment

(required) 
(required) 
(optional)
(required)