背景:
1.有域名aa.com,域名bb.com;
2.另有一段文本内容,其中可能有包含"aa.com"或者"bb.com"(也可能是二级域名)的链接或者地址。 需求:
如果文本中包含aa.com,bb.com的字符串是链接,则不处理;如果不是链接,将此url加上新页面打开的链接。 例子:
文本1为:请登录www.a.aa.com/xxx.htm ,将www.a.aa.com/xxx.htm 加上链接。则处理为<a href='www.aa.com/xxx.htm' target='_blank'>www.a.aa.com/xxx.htm</a>
文本2为:请登录<a href='http://www.a.aa.com/xxx.htm'>xxx</a> 则跳过不处理
1.有域名aa.com,域名bb.com;
2.另有一段文本内容,其中可能有包含"aa.com"或者"bb.com"(也可能是二级域名)的链接或者地址。 需求:
如果文本中包含aa.com,bb.com的字符串是链接,则不处理;如果不是链接,将此url加上新页面打开的链接。 例子:
文本1为:请登录www.a.aa.com/xxx.htm ,将www.a.aa.com/xxx.htm 加上链接。则处理为<a href='www.aa.com/xxx.htm' target='_blank'>www.a.aa.com/xxx.htm</a>
文本2为:请登录<a href='http://www.a.aa.com/xxx.htm'>xxx</a> 则跳过不处理
"
source = Regex.Replace(source, @"(?i)(?<!<a\s+href=(['""])?.*?)(?:https?://)?www\.a\.(?:aa|bb)\.com[^'""\s]+", "<a href='$0' target='_blank'>$0</a>");
Response.Write(source);
另外求大神推荐一些学习正则的书或者链接吧。感觉作为一个伸手党很惭愧。
太麻烦了点吧。
我这有本武林秘籍,就送你吧: http://download.csdn.net/detail/q107770540/2844387
{
string domainReg = string.Empty;
string[] domains = { "aa.com", "bb.com", "tmall.com" };
foreach (string domain in domains)
{
domainReg += domain + "|";
}
if (!string.IsNullOrEmpty(domainReg))
{
domainReg = domainReg.TrimEnd('|');
} string source = "www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 www.aaaa.aa.com/xxx.aspx 123123<br />123123123www.aa.com<br />http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=";
string source2 = Regex.Replace(source, string.Format(@"(?is)(?<!<a\b\s+href=(['""]))((?:http(s)?://)|(?:www\.))(\w+\.)?({0})([^'""<\s]+)?", domainReg), "<a href='http://$0' target='_blank'>$0</a>"); Response.Write("加链接前:" + source);
Response.Write("<br />");
Response.Write("加链接后:" + source2);
}
void Main()
{
string[] domains = { "aa.com", "bb.com", "tmall.com" };
string domainReg = string.Join("|",domains);
string pattern=@"(?i)(?<!<a\s+href=(['""])?(?:https?://)?)(?:https?://)?www\.(\w+\.)?(?:{0})[^'""<\s]*";
string source = "www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 www.aaaa.aa.com/xxx.aspx 123123<br />123123123www.aa.com<br />http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=";
string source2 = Regex.Replace(source, string.Format(pattern,domainReg),"<a href='http://$0' target='_blank'>$0</a>"); Response.Write("加链接前:" + source);
Response.Write("<br />");
Response.Write("加链接后:" + source2);
/*
加链接前:www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 www.aaaa.aa.com/xxx.aspx 123123<br />123123123www.aa.com<br />http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=
加链接后:www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 <a href='http://www.aaaa.aa.com/xxx.aspx' target='_blank'>www.aaaa.aa.com/xxx.aspx</a> 123123<br />123123123<a href='http://www.aa.com' target='_blank'>www.aa.com</a><br /><a href='http://http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=' target='_blank'>http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=</a>
*/
@"(?i)(?<!<a\s+href=['""]?[^'""<\s,;|]*)((?:((https:|http:)?//))?|(www\.)?)(\w+\.)?(?:{0})[^'""<\s,;|\uFF00-\uFFFF\u4e00-\u9fa5]*"