Reputation: 1924
In html need replace url to tag
Only http://google3.com:1139 and http://google6.com:1139
<div>
<a href="http://google1.com:1139" target="_blank">http://google2.com:1139</a>
http://google3.com:1139
</div>
<div>
<a href="http://google4.com:1139" target="_blank">http://google5.com:1139</a>
http://google6.com:1139
</div>
Must be
<div>
<a href="http://google1.com:1139" target="_blank">http://google2.com:1139</a>
<a href="http://google3.com:1139" target="_blank">http://google3.com:1139</a>
</div>
<div>
<a href="http://google4.com:1139" target="_blank">http://google5.com:1139</a>
<a href="http://google6.com:1139" target="_blank">http://google6.com:1139</a>
</div>
var result = Regex.Replace("<div><a href=\"http://google1.com:1139\" target=\"_blank\">http://google2.com:1139</a>http://google3.com:1139</div><div><a href=\"http://google4.com:1139\" target=\"_blank\">http://google5.com:1139</a>http://google6.com:1139</div>",
@"((?<!href=['""]?)(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?)",
"<a target='_blank' href='$1'>$1</a>");
But need replace
started with http but now with href="http (found)
not ended with
</a>
<a ... </a>
Upvotes: 0
Views: 553
Reputation: 9650
I take it the requirements may be rephrased as follows:
<a
and </a>
should be left intact (this includes href
attribute values)<a
and </a>
should be wrapped in anchor tags.This may be achieved by searching two patterns, <a.*?</a>
and <some URL>, as alternatives. Then replace the match by itself if the first pattern is found and by a wrapped URL if the second pattern found:
Regex.Replace(html,
@"<a.*?</a>|(?:https?|ftp)://[\w_.-]+:\d+",
m => m.Value.StartsWith("<")
? m.Value
: string.Format("<a target='_blank' href='{0}'>{0}</a>", m.Value));
Demo: https://ideone.com/Jq1s8y
P.S.
I simplified the URL regex for the sake of conciseness. The real application may require more extended pattern.
Upvotes: 1