I'm trying to replace plain domain like substrings of a input string with 'a' tags, using regex like this:
var pattern = @"[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})";
var input = "text1 www.example.com text2 <a href='foo'>www.example.com</a> text3";
var result = Regex.Replace(input, pattern, string.Format("<a href='$0'>$0</a>"));
This will create following output:
text1 <a href='www.example.com'>www.example.com</a> text2 <a href='foo'><a href='www.example.com'>www.example.com</a></a> text3
Which is wrong as second domain is already tag and it is now tag within tag.
Is there a way to modify regex pattern to ignore matching of second domain substring?
Perhaps by ignoring the '>' char at domain substring start? (or '<' char at the end)
Effectively generating this result:
text1 <a href='www.example.com'>www.example.com</a> text2 <a href='foo'>www.example.com</a> text3