I have to replace specific links with the link which I pre-defined.
string content = @"<html>"
+" <a href='http://www.yourdomain.com' style='width:10px;'>Click Here</a>"
+" <br>"
+" <a href='http://www.yourdomain.com/products/list.aspx' style='width:10px;'>Click Here</a>"
+" </html>";
To be replaced like this ===> "http://www.yourdomain.com"
to "http://www.mydomain.com"
But I don't want other link which also start with "http://www.yourdomain.com"
to be replaced. If these links has a sub link (i.e., "/products/list.aspx"
).
So I use C# string.Replace()
function first.
string result = content.Replace("http://www.yourdomain.com", "http://www.mydomain.com");
I also try Regex.Replace()
function as well.
string pattern = @"\bhttp://www.yourdomain.com\b";
string replace = "http://www.mydomain.com";
string result = Regex.Replace(content, pattern, replace);
But I got same result. like below.
<html>
<a href='http://www.mydomain.com' style='width:10px;'>Click Here</a>
<br>
<a href='http://www.mydomain.com/products/list.aspx' style='width:10px;'>Click Here</a>
</html>
What I want is like below.
<html>
<a href='http://www.mydomain.com' style='width:10px;'>Click Here</a>
<br>
<a href='http://www.yourdomain.com/products/list.aspx' style='width:10px;'>Click Here</a>
</html>
Update
According to @Robin suggestion, my problem is solved.
string content = @"<html>"
+" <a href='http://www.yourdomain.com' style='width:10px;'>Click Here</a>"
+" <br>"
+" <a href='http://www.yourdomain.com/products/list.aspx' style='width:10px;'>Click Here</a>"
+" </html>";
string pattern = string.Format("{0}(?!/)", "http://www.yourdomain.com");
string replace = "http://www.mydomain.com";
string result = Regex.Replace(content, pattern, replace);
Update
Another alternative way which I found is
http://www.yourdomain.com([^/])