XML로 컨텐츠를 변환 한 후 단순히 당신이 XPath를 사용하여 사용해야합니다 mailto:
로 시작하는 href
를 포함하는 a
태그 검색 : 당신이있는 경우 using System.Xml.XPath;
var content = "Lorem ipsum dolor sit amet <a href=\"mailto:[email protected]\">[email protected]</a> ipsum dolor <a href=\"mailto:[email protected]\">[email protected]</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());
정규 표현식은 여기에 친구가 될 것입니다. 이것은 Stackoverflow에서 여러 번 다루어 졌으므로 닫히지 않을까 걱정하지 마십시오. –
자,이 질문을 다루는 게시물이 있습니까? 그렇다면, 나는 그것을 읽게되어 매우 행복 할 것이다. ;-) – Nicke
이 스레드의 두 번째 대답은 도움이 될 수 있습니다. - http://stackoverflow.com/questions/4718933/regular-expression-to-match-a-href-mailto –