C#正则让文本中的所有网址自动变成连接
来源:互联网 发布:手机迅雷连不上网络 编辑:程序博客网 时间:2024/06/05 17:34
public static string ShowUrls(string text)
{
Regex linkRegex = new Regex(" href\\s*=\\s*(?:(?:\\\"(?<url>[^\\\"]*)\\\")|(?<url>[^\\s]* ))",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
string clearText = Regex.Replace(text, "<[^>]*>",string.Empty, RegexOptions.Compiled);//清除html标记
matchs = Regex.Matches(clearText, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
bool flag = true;
foreach (Match m in matchs)
{
string link = "<a href=\"" + m.ToString() + "\" target=\"_new\">" + m.ToString() + "</a>";
if (linkMatchs.Count > 0)
{
foreach (Match linkMatch in linkMatchs)
{
if (linkMatch.Value.IndexOf(m.Value) > -1)
{
flag = false;
break;
}
}
}
if(flag)
{
text = text.Replace(m.ToString(), link);
}
}
return text;
{
Regex linkRegex = new Regex(" href\\s*=\\s*(?:(?:\\\"(?<url>[^\\\"]*)\\\")|(?<url>[^\\s]* ))",
RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection linkMatchs = linkRegex.Matches(text);
string pattern = "";
if (text.Contains("http:"))//匹配http:
{
pattern = @"(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])";
}
else {//匹配www:
pattern = @"[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])";
}
string clearText = Regex.Replace(text, "<[^>]*>",string.Empty, RegexOptions.Compiled);//清除html标记
matchs = Regex.Matches(clearText, pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
bool flag = true;
foreach (Match m in matchs)
{
string link = "<a href=\"" + m.ToString() + "\" target=\"_new\">" + m.ToString() + "</a>";
if (linkMatchs.Count > 0)
{
foreach (Match linkMatch in linkMatchs)
{
if (linkMatch.Value.IndexOf(m.Value) > -1)
{
flag = false;
break;
}
}
}
if(flag)
{
text = text.Replace(m.ToString(), link);
}
}
return text;
}
- C#正则让文本中的所有网址自动变成连接
- 匹配文本中的网址java正则表达式
- 匹配文本中的网址java正则表达式
- java正则表达式 匹配文本中的网址
- 匹配文本中的网址java正则表达式
- 正则匹配所有的网址
- C#中的多线程 网址
- 让ul中的li变成横向排列
- 让网页选择的文本背景变成红色.
- C# 获取收藏夹中所有网址代码
- NSDataDetector检测文本中的url网址
- 让网页所有的样式都变成灰色
- 有些时候文本变成一竖行,自动换行丢失
- Android中TextView自动连接网址
- C#正则截取URL网址字符串
- C#正则截取URL网址字符串
- 第一篇博文:TXT文本中找出所有网址
- 使用正则表达式自动为文本中的url地址生成链接
- 获取网页内容 C和php通过socket进行通信
- android从一个activity跳到另一个activity 报错 经验总结
- 谷安天下CISA培训——2012情人节特惠
- C++ 编程遇到的一些问题
- android的binder机制研究(C++部分)
- C#正则让文本中的所有网址自动变成连接
- 在android源码中添加库模块&&jni实例二
- iPhone开发中如何异步调用web service
- Jboss告警
- 就是这么一个小店?
- 黑马程序员---委托和事件
- UIWebView中链接调用Safari打开目标链接 点击cell里面的一个按钮如何确定按钮所在的cell的行
- 字符串和整数的比较
- linux进程描述符task_struct详解