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 ...
随机推荐
- shell_基础知识
参考: http://blog.csdn.net/kaizi318/article/details/9343551 开头程序必须以下面的行开始(必须方在文件的第一行):#!/bin/sh符号#!用来告 ...
- 通过WinForm控件创建的WPF控件无法输入的问题
今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...
- 8.springMVC中的RESTful架构风格
RESTful架构:是一种设计的风格,并不是标准,只是提供了一组设计原则和约束条件,也是目前比较流行的一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. 关于 ...
- C++ MFC获取软件运行目录 (包含软件名)
TCHAR *path = new TCHAR[MAX_PATH]; GetModuleFileName(NULL,path,MAX_PATH); AfxMessageBox(path);
- C#控件根据窗体改变大小
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...
- php设计模式学习之观察者模式
什么都不说,先看代码: interface userOperateImpl { public function operate($username); } class userLoginLog imp ...
- Android Log介绍
android.util.Log常用的方法有以下5个:Log.v() ,Log.d() ,Log.i() ,Log.w() ,Log.e() .按照日志级别从高到低为ERROR, WARN, INFO ...
- Apache配置默认首页面
conf -> httpd.conf下设置成 <IfModule dir_module> DirectoryIndex index.php index.html index.htm ...
- Nginx虚拟目录支持PHP配置
感谢作者:http://blog.csdn.net/fangaoxin/article/details/7030139 location ~ ^/test/.+\.php$ { alias /var/ ...
- struts2获取web元素的方式和方法
获取web资源的方式按是否与servlet耦合可分为两种,再细分按照依赖方式又有两种即 依赖容器 和 依赖注入 什么是依赖容器 就是依赖 ActionContext或者ServletActionC ...