正则匹配连接
2017-07-31 19:02
155 查看
上码:
string[] imgtag = { ".gif", ".jpg", ".png", ".jpeg" };
string tpllink = "<a target=\"_blank\" style=\"color:#e25252;text-decoration: underline;\" href='{0}'>{1}</a>";
Regex re = new Regex(@"((http|https)://)?(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[^<|^>]*)?");
MatchCollection matchCollection = re.Matches(txtmsg);
HashSet<string> urlKeys = new HashSet<string>();
foreach (Match m in matchCollection)
{
if (imgtag.Any(x => m.Value.Contains(x)))
{
continue;
}
if (urlKeys.Add(m.Value))
{
txtmsg = txtmsg.Replace(m.Value, String.Format(tpllink, m.Value.Contains("http://") ? m.Value : "http://" + m.Value, m.Value));
}
}
string[] imgtag = { ".gif", ".jpg", ".png", ".jpeg" };
string tpllink = "<a target=\"_blank\" style=\"color:#e25252;text-decoration: underline;\" href='{0}'>{1}</a>";
Regex re = new Regex(@"((http|https)://)?(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[^<|^>]*)?");
MatchCollection matchCollection = re.Matches(txtmsg);
HashSet<string> urlKeys = new HashSet<string>();
foreach (Match m in matchCollection)
{
if (imgtag.Any(x => m.Value.Contains(x)))
{
continue;
}
if (urlKeys.Add(m.Value))
{
txtmsg = txtmsg.Replace(m.Value, String.Format(tpllink, m.Value.Contains("http://") ? m.Value : "http://" + m.Value, m.Value));
}
}
相关文章推荐
- IOS 正则表达式匹配文本中URL位置并获取URL所在位置(解决连接中文问题)
- IOS 正则表达式匹配文本中URL位置并获取URL所在位置(解决连接中文问题)
- Java学习笔记-正则表达式的模式匹配
- java 正则表达式匹配中文
- PHP正则匹配汉字、字母、数字和下划线,不含特殊字符
- 百家姓 正则 匹配。
- 常用正则表达式大全!(例如:匹配中文、匹配html)
- 使用正则匹配字符串的子串
- Java的匹配正则表达式Pattern和Matcher
- 匹配中文字符的正则表达式: [u4e00-u9fa5]
- 关于正则表达式匹配无异常资源耗尽的解决方案
- jsva程序:正则表达式匹配域名
- HDU 6170 正则表达式匹配
- 利用正则表达式取得匹配上的字符
- Java 正则表达式--匹配字符获取、IP地址排序
- 正则匹配[ ]中内容适用于replace
- 剑指offer—正则表达式匹配
- java中的正则表达式匹配
- 正则表达式匹配英文和法文
- 正则表达式匹配解析过程探讨分析(正则表达式匹配原理)