转载: http://blog.csdn.net/weasleyqi/article/details/7912647

首先,正则表达式:

String check = @"((http|ftp|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})*(/[a-zA-Z0-9\&%_\./-~-]*)?";

关于该正则表达式的说明:

①:该正则表达式匹配的字符串必须以http://、https://、ftp://开头;

②:该正则表达式能匹配URL或者IP地址;(如:http://www.baidu.com 或者 http://192.168.1.1)

③:该正则表达式能匹配到URL的末尾,即能匹配到子URL;(如能匹配:http://www.baidu.com/s?wd=a&rsv_spt=1&issp=1&rsv_bp=0&ie=utf-8&tn=baiduhome_pg&inputT=1236)

④:该正则表达式能够匹配端口号;

阻止某些指定的URL:

如果我们在输入的文本框中要阻止http://www.baidu.com这个URL,传统的方法是用上面的正则表达式去匹配文本框中的URL,读出所有的URL之后在跟要阻止的URL去比较,但是这种方法有一个弊端,就是我们读出的URL是一直到子URL,而我们在配置文件中可能就写一个母URL,这样要对check出来的URL进行切割,加上网站默认的端口号是:80,我们要去比较端口号等等,我想出了一种新的方法:

从配置文件中读出要阻止的URL,组成一个正则表达式,去匹配文本框中,如果能够匹配到,则阻止掉。

配置文件中应该写:<add key="DomainCheckBlackUrl" value="baidu.com" />

在代码中实现:

现在一个正则表达式由3部分组成:

1:正则表达式的开头,可能由任意的字符组成;

2:正则表达式的中间部分:从配置文件中读出来的部分;

3:正则表达式的结尾部分:可能有一些子目录或者端口号等;

首先,从配置文件中读出URL:string[] serverlist = ConfigurationManager.AppSettings["DomainCheckBlackUrl"].Split(',');(配置文件中以“,”分割)

其次,string start = @"((http|ftp|https)://)([a-zA-Z0-9_-]+\.)*";(正则表达式的开头)

然后,正则表达式的结尾: end = @"(:[0-9]{1,4})?((/[a-zA-Z0-9\&%_\./-~-]*)|(?=[^a-zA-Z0-9\.]))";

组合之后的正则表达式: string check = start + @"((?<=[^a-zA-Z0-9])(" + CutStr + "))" + end;

URL Regex expression的更多相关文章

  1. Regex Expression的资料和笔记整理

    维基百科:http://en.wikipedia.org/wiki/Regular_expression 正则表达式在线测试:http://tool.chinaz.com/regex/ 正则表达式,常 ...

  2. [Regex Expression] Find Sets of Characters

    Regular Expression Character Classes define a group of characters we can use in conjunction with qua ...

  3. boost regex expression

    Boost.Regex provides three different functions to search for regular expressions 1. regex_match #inc ...

  4. [Regex Expression] Use Shorthand to Find Common Sets of Characters

    In this lesson we'll learn shorthands for common character classes as well as their negated forms. v ...

  5. [Regex Expression] Tagline --- {0, } {1,10}

    Using a character set repeated 1 or more times, make a pattern to search for strings that do not con ...

  6. [Regex Expression] Confirmative -- World bundry

    String to check: As it turns out, our potential shipmates are extremely superstitious. As such, we d ...

  7. [Regex Expression] Match mutli-line number, number range

    /^-?\d{,}\.\d+$/gm

  8. java Suspicious regex expression "." in call to 'replaceAll()' 问题延展

    因为要处理从身份证读取到的有效期时间,所以用到了replaceAll这个方法,类似如下代码: String s1 = s.replaceAll(".", "-" ...

  9. C# 网络编程之webBrowser获取网页url和下载网页中图片

    该文章主要是通过C#网络编程的webBrowser获取网页中的url并简单的尝试瞎子啊网页中的图片,主要是为以后网络开发的基础学习.其中主要的通过应用程序结合网页知识.正则表达式实现浏览.获取url. ...

随机推荐

  1. JS实现HashMap

    /** * ********* 操作实例 ************** * var map = new HashMap(); * map.put("key1","Valu ...

  2. iOS纯代码适配masonry中mas_的问题

    //equalto 和 mas_equalto 是有区别的.但是我们不打算去了解,可以通过添加以下代码来统一. //注意!! 宏定义必须要放在 import 引入头文件之前! //define thi ...

  3. jQuery的选择器中的通配符使用介绍

    $("input[id^='data']");//id属性以data开始的所有input标签 $("input[id$='data']");//id属性以dat ...

  4. mysql之替换字符串

    update `wp_posts` set `post_content`=REPLACE(`post_content`,'localhost/linkcp','www.linkcp.cn') wher ...

  5. Android Saving Data(一)

    Saving Key-value Sets  保存键值对 SharedPreferences只能用来保存一些简单的数据,并且这些数据可以是共享的,也可以是私有的. SharedPreferences没 ...

  6. (转)AppiumLibrary基本操作

    *** Settings *** Library AppiumLibrary Library Collections Library String Library Dialogs *** Test C ...

  7. iOS_Quartz2D之涂鸦板

    响应者对象:继承了UIResponder的对象 触摸事件:一根或多根手指: 开始触摸: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent ...

  8. weex环境搭建

    1. 安装weex-toolkit sudo npm install -g weex-toolkit 装完之后就可以使用weex命令了.输入weex命令可以看到: weex test.we --qr ...

  9. zend studio 配置 apache服务器事宜

    安装好 zend studio后,配置 apache服务器时,设置 configuration directory时,需选中 xampp\apache里面的 conf 文件夹,即完整的路径为: *\x ...

  10. git下载自己项目到本地

    git下载自己项目到本地 假如外出工作,需要在另一台电脑上面pull自己的某个git远程项目到本地 $ git init $ git pull https://github.com/TTyb/54qj ...