网上关于Url转链接(href)的正则表达式一搜一大堆,但真正好用的没几个。

后来在Matthew O'Riordan的Blog上发现一个很好用的正则表达式,是用Javascript写的,代码如下:

(
( // brackets covering match for protocol (optional) and domain
([A-Za-z]{3,9}:(?:\/\/)?) // match protocol, allow in format http:// or mailto:
(?:[\-;:&=\+\$,\w]+@)? // allow something@ for email addresses
[A-Za-z0-9\.\-]+ // anything looking at all like a domain, non-unicode domains
| // or instead of above
(?:www\.|[\-;:&=\+\$,\w]+@) // starting with something@ or www.
[A-Za-z0-9\.\-]+ // anything looking at all like a domain
)
( // brackets covering match for path, query string and anchor
(?:\/[\+~%\/\.\w\-]*) // allow optional /path
?\??(?:[\-\+=&;%@\.\w]*) // allow optional query string starting with ?
#?(?:[\.\!\/\\\w]*) // allow optional anchor #anchor
)? // make URL suffix optional
)

针对我们的使用场景(只对http或https开头的Url进行转换)简化了一下,并用C#写出:

public static class ContentFormatter
{
private static readonly Regex Url_To_Link = new Regex(@"(?<url>
(https?:(?:\/\/)?) # match protocol, allow in format http:// or https://
[A-Za-z0-9\.\-]+ # anything looking at all like a domain, non-unicode domains
( # brackets covering match for path, query string and anchor
(?:\/[\+~%\/\.\w\-]*)? # allow optional /path
\??(?:[\-\+=&;%@\.\w]*?) # allow optional query string starting with ?
\#?(?:[\.\!\/\\\w\-]*) # allow optional anchor #anchor
)? # make URL suffix optional
)",
RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace,
TimeSpan.FromMilliseconds(100)); public static string UrlToLink(string text)
{
if (string.IsNullOrEmpty(text)) return string.Empty; return Url_To_Link.Replace(text, "<a href=\"${url}\" target=\"_blank\">${url}</a>");
}
}

Url转Link的C#正则表达式的更多相关文章

  1. java通过url抓取网页数据-----正则表达式

    原文地址https://www.cnblogs.com/xiaoMzjm/p/3894805.html [本文介绍] 爬取别人网页上的内容,听上似乎很有趣的样子,只要几步,就可以获取到力所不能及的东西 ...

  2. JS获取url中query_str JavaScript RegExp 正则表达式基础详谈

    面我们举例一个URL,然后获得它的各个组成部分:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.window.location.href(设置或获取整个 URL ...

  3. url映射 ccf (Java正则表达式80分解法)

    问题描述 试题编号: 201803-3 试题名称: URL映射 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 URL 映射是诸如 Django.Ruby on Rails 等 ...

  4. Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码

    通过 正则表达式 来获取一个网页中的所有的 URL链接,并下载这些 URL链接 的源代码 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 ...

  5. [python] 常用正则表达式爬取网页信息及分析HTML标签总结【转】

    [python] 常用正则表达式爬取网页信息及分析HTML标签总结 转http://blog.csdn.net/Eastmount/article/details/51082253 标签: pytho ...

  6. URL重写:RewriteCond指令与RewriteRule 指令格式(转)

    Rewirte主要的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范.平时帮助我们实现拟静态,拟目录,域名跳转,防止盗链等.本文将针对mod_rewrite和URL匹配的技术细 ...

  7. iOS 检测文本中的 URL、电话号码等信息

    iOS 检测文本中的 URL.电话号码等信息 要检测文本中的 URL.电话号码等,除了用正则表达式,还可以用 NSDataDetector. 用 NSTextCheckingResult.Checki ...

  8. Apache URL重写规则

    1.简介 Apached的重写功能,即是mod_rewrite模块功能,它是apache的一个模块.它的功能非常强大,可以操作URL中的所有部分. 因此我们就可以改写url,给用户提供一个简介大方的u ...

  9. 常用正则表达式爬取网页信息及HTML分析总结

    Python爬取网页信息时,经常使用的正则表达式及方法. 1.获取<tr></tr>标签之间内容 2.获取<a href..></a>超链接之间内容 3 ...

随机推荐

  1. 通过Percona XtraBackup添加一个slave

    假设我的master是172.16.15.8,要添加的slave是172.16.15.9 1:在主服务器执行备份操作(172.16.15.8) /usr/bin/innobackupex --comp ...

  2. C# 定制 Attribute 简单使用

    所谓 “定制Attribute”,就是一个类的实例,它被序列化成驻留在元数据的一个字节流. 我们可以使用 Attribute 来保存注释: namespace AttributeDemo { [Att ...

  3. IE内嵌google chrome frame解决浏览器兼容问题

    IE内嵌google chrome frame解决浏览器兼容问题  http://www.cnblogs.com/xwdreamer/archive/2013/12/17/3477776.html 参 ...

  4. 使用CocoaPods配置工程

    1.首先搭建环境,配置CocoaPods,具体请参考 http://code4app.com/article/cocoapods-install-usage 2.打开终端,输入 cd 空格 把工程拖入 ...

  5. asp.net GridView控件的列属性

    BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...

  6. Spring RMI (Spring 远程方法调用)

    所需jar包...?    不纠结,一股脑儿全导! 源码地址:http://pan.baidu.com/s/1jG8eOmy 先放结构图如下,客户端和服务端都在一个项目中.也可以把服务端的xxx导成j ...

  7. Xml文件操作的其中一个使用方法:

    XmlNodeList students = doc.DocumentElement.ChildNodes;//Student节点集合 foreach (XmlNode stu in students ...

  8. html5 -js判断undefined类型

    js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined所以自作聪明判断 var reVal ...

  9. java.net.MalformedURLException: Illegal character in URL

    在进行接口测试时,意外发现返回结果报java.net.MalformedURLException: Illegal character in URL,意思是“在URL中的非法字符”,我的参数是经过ba ...

  10. HTML中doctype以及target论述

    首先,为什么要在每个html文档开头写入<!doctype......>呢.                                                         ...