Dee J. Doena
Dee J. Doena

Reputation: 1843

Regex Replace with named group and everything else around

Imagine I had a regex that is supposed to capture an HTML link and replace it with another link.

The Regex would look something like this:

<a.+?href="(?'url'.+?)".*?>

It would match HTML tags like this:

<a href="http://www.google.com">

or this:

<a target="_blank" href="#top">

If I now call

regex.Replace(new MatchEvaluator(ReplaceUrl))

I would get a parameter Match and within the named group.

But the Match.Groups collection does not just contain 3 items.

How do I put the entire match back together, so the result would be

<a href="http://www.EvulRulz.devil">

Or do I really need to put everything else into a named group as well?

Thanks to Jonesy (see below I've finally grasped the meaning of the indexes in the Groups collection,

All unnamed groups are simply numbered starting from 1. They appear in the same order as they were in the regex pattern.

So my replace method will look like this (based on the unmodified regex which I will actually adapt):

private static String ReplaceUrl(Match match)
{
    return (match.Groups[1].Value + "http://www.EvulRulz.devil" + match.Groups[2].Value);
}

Upvotes: 0

Views: 581

Answers (1)

Jonesopolis
Jonesopolis

Reputation: 25370

use capture groups to grab the other parts of the tag too:

(<a.+?href=")([^"]+)("[^>]*>)

then plug those back in, in the replace:

var pattern = @"(<a.+?href="")([^""]+)(""[^>]*>)";
var str = @"<a target=""_blank"" href=""#top"">";

var newUrl = "http://www.stackoverflow.com";
var newTag = Regex.Replace(str, pattern, "$1" + newUrl + "$3");

(I changed your base Regex a bit)

Upvotes: 1

Related Questions