2

I've got this example string:

var content = "Lorem ipsum dolor sit amet <a href=\"mailto:info@xxx.com\">info@xxx.com</a> ipsum dolor <a href=\"mailto:info@yyy.eu\">info@yyy.eu</a> adipiscing elit.";

I want to replace (manipulate) all occurrences of the of the a-tag when the string contains a mailto-link.

The anchor's should look like after manipulation:

<a href="#" title="protected">in..@xxx.com</a>

The content string could contain any number of mailto-links.

How can I best perform this task?

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
Nicke
  • 365
  • 4
  • 8
  • 25
  • 4
    Regex is going to be your friend here. This has been covered many times on Stackoverflow, so don't be surprised this gets closed. – Nick Berardi Jul 01 '12 at 11:09
  • Ok, do you have a post that covers this question? if so, I would be very happy to read it. ;-) – Nicke Jul 01 '12 at 11:55
  • 2nd answer in this thread might be helpful - http://stackoverflow.com/questions/4718933/regular-expression-to-match-a-href-mailto – Jeremy Wiggins Jul 01 '12 at 13:57

2 Answers2

2

If you have XHTML then use XElement as Chuck showed.

If not, then regular expressions are the way to go. Something like:

Regex find = new Regex("<a\\b[^>]*href=['\"]mailto:(.*?)['\"]", RegexOptions.Singleline | RegexOptions.IgnoreCase);

Warning, I did not test the above code but I'm 99% certain it is correct. Also, I may have missed a corner case such as a > in the email address.

David Thielen
  • 28,723
  • 34
  • 119
  • 193
0

Convert the content to XML and then simply search for the a tags that contain an href that starts with mailto:

You'll need this using to use XPath: using System.Xml.XPath;

var content = "Lorem ipsum dolor sit amet <a href=\"mailto:info@xxx.com\">info@xxx.com</a> ipsum dolor <a href=\"mailto:info@yyy.eu\">info@yyy.eu</a> adipiscing elit.";

XElement x = XElement.Parse(string.Format("<root>{0}</root>", content));
var hrefs = x.XPathSelectElements("a[starts-with(@href, 'mailto:')]");
foreach (XElement href in hrefs)
{
    href.Attribute("href").Value = "#";
    href.Add(new XAttribute("title", "protected"));

    string email = href.Value;
    int at = email.IndexOf('@');
    if(at > 0)
    {
        string username = email.Substring(0, at);
        string domain = email.Substring(at);
        if (username.Length > 2)
            href.Value = string.Format("{0}..{1}", 
                username.Substring(0, 2), domain);
    }
}
string result = string.Concat(x.Nodes().ToArray());
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69