Url转Link的C#正则表达式
网上关于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#正则表达式的更多相关文章
- java通过url抓取网页数据-----正则表达式
原文地址https://www.cnblogs.com/xiaoMzjm/p/3894805.html [本文介绍] 爬取别人网页上的内容,听上似乎很有趣的样子,只要几步,就可以获取到力所不能及的东西 ...
- JS获取url中query_str JavaScript RegExp 正则表达式基础详谈
面我们举例一个URL,然后获得它的各个组成部分:http://i.cnblogs.com/EditPosts.aspx?opt=1 1.window.location.href(设置或获取整个 URL ...
- url映射 ccf (Java正则表达式80分解法)
问题描述 试题编号: 201803-3 试题名称: URL映射 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 URL 映射是诸如 Django.Ruby on Rails 等 ...
- Python 网络爬虫 009 (编程) 通过正则表达式来获取一个网页中的所有的URL链接,并下载这些URL链接的源代码
通过 正则表达式 来获取一个网页中的所有的 URL链接,并下载这些 URL链接 的源代码 使用的系统:Windows 10 64位 Python 语言版本:Python 2.7.10 V 使用的编程 ...
- [python] 常用正则表达式爬取网页信息及分析HTML标签总结【转】
[python] 常用正则表达式爬取网页信息及分析HTML标签总结 转http://blog.csdn.net/Eastmount/article/details/51082253 标签: pytho ...
- URL重写:RewriteCond指令与RewriteRule 指令格式(转)
Rewirte主要的功能就是实现URL的跳转和隐藏真实地址,基于Perl语言的正则表达式规范.平时帮助我们实现拟静态,拟目录,域名跳转,防止盗链等.本文将针对mod_rewrite和URL匹配的技术细 ...
- iOS 检测文本中的 URL、电话号码等信息
iOS 检测文本中的 URL.电话号码等信息 要检测文本中的 URL.电话号码等,除了用正则表达式,还可以用 NSDataDetector. 用 NSTextCheckingResult.Checki ...
- Apache URL重写规则
1.简介 Apached的重写功能,即是mod_rewrite模块功能,它是apache的一个模块.它的功能非常强大,可以操作URL中的所有部分. 因此我们就可以改写url,给用户提供一个简介大方的u ...
- 常用正则表达式爬取网页信息及HTML分析总结
Python爬取网页信息时,经常使用的正则表达式及方法. 1.获取<tr></tr>标签之间内容 2.获取<a href..></a>超链接之间内容 3 ...
随机推荐
- rediscluster 集群操作(摘抄)
一:关于redis cluster 1:redis cluster的现状 目前redis支持的cluster特性 1):节点自动发现 2):slave->master 选举,集群容错 3):Ho ...
- codevs1230 元素查找
1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 给出n个正整数,然后有m个询问,每 ...
- Java值传递和引用传递详细解说
前天在做系统的时候被Java中参数传递问题卡了一下,回头查阅了相关的资料,对参数传递问题有了新的了解和掌握,但是有个问题感觉还是很模糊,就是 Java中到底是否只存在值传递,因为在查阅资料时,经常看到 ...
- Origin9.1如何使用原始数据(Raw Data)绘制风向玫瑰图
核心提示:今天为大家简单介绍下如何使用原始数据绘制风向玫瑰图.本例以Origin 9.1进行演示.1.本例所用数据截图如下,列A为风向,列B为风速.2.选中两列数据,进入Plot下的Specializ ...
- 接收 ajax POST 方式传入的参数
----前台--- var list = new Array(); var params = { gencodeid : "test001", value : "01&q ...
- extjs,清空treepanel数据。
extjs,清空treepanel数据. //调用 var rootNode = tree.getRootNode(); removeChildrenData(rootNode); //清理节点的数据 ...
- web工程中URL地址的推荐写法
三.web工程中URL地址的推荐写法 使用c标签<c:url value="" /> 会自动添加项目名 -> value中的值 前面要加 “/” 在JavaWeb ...
- js完美转换阿拉伯数字为数字大写(原创)
啥都不说,直接上代码: //阿拉伯数字转换为简写汉字 function Arabia_To_SimplifiedChinese(Num) { for (i = Num.length - 1; i &g ...
- mormot THttpApiServer使用例子
mormot THttpApiServer使用例子 THttpApiServer封装了WINDOWS的HTTPS.SYS. unit Unit1; interface uses Winapi.Wind ...
- python学习之路-day5-模块
本节内容: 模块详解 1.模块定义 2.os&sys模块 3.time&datetime模块 4.random模块 5.shutil模块 6.shelve模块 7.configpars ...