I am having following set of image map tags;
<map name="planetmap">
<area shape="rect" coords="0,0,82,126" href="sun.htm" alt="Sun">
<area shape="circle" coords="90,58,3" href="mercur.htm" alt="Mercury">
<area shape="circle" coords="124,58,8" href="venus.htm" alt="Venus">
</map>
I need to extract href attribute and replace it with another url
I am using following code but does not seems to work;
string input = @"<area shape=""rect"" href=""http://www.google.com"">";
string pattern = "(href=([^ ]+))";
Regex rgx = new Regex(pattern);
string result2 = rgx.Replace(input, m => m.Groups[1].Value.Replace(result,"test.com"));
Could someone please help me.
Thanks
I can see two things that may be causing an issue. The first is, your regex is checking for a string in the format of
href=http://example.com. Note, there are no quotes around the URL. So, we’ll need to update the regex to handle the double-quotes like your input contains.The second is that you’re using the matched group
1, but your regex is actually matching two separate groups (and you want to replace the second one). You don’t need the first matching group at all, so we can actually drop this part.In all, try updating your regex to:
In your variable, it will look like: