• 所有Url的正则表达式,用来验证返回的Url是否符合RFC1738规定


    from:http://www.pin5i.com/showtopic-25932.html

    在没有了解RFC1738的时候,一直以为Url的正则表达式很简单,没想到Url有这么多分类,更没想到一个普通的http的正则表达式也不是那么简单。

    以下是我搜到的关于http的正则表达式:

    1. http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
    复制代码

    当然这已经满足大部分人的需求了,但是如果需要严格的验证的话还是要符合RFC1738了。

    Url包括Http,Ftp,News,Nntpurl,Telnet,Gopher,Wais,Mailto,File,Prosperurl和Otherurl。

    呵呵,废话不多说了,上代码

    1. #region Http

    2.             string lowalpha = @"[a-z]";
    3.             string hialpha = @"[A-Z]";
    4.             string alpha = String.Format(@"({0}|{1})", lowalpha, hialpha);
    5.             string digit = @"[0-9]";
    6.             string safe = @"(\$|-|_|\.|\+)";
    7.             string extra = @"(!|\*|'|\(|\)|,)";
    8.             string hex = String.Format(@"({0}|A|B|C|D|E|F|a|b|c|d|e|f)", digit);
    9.             string escape = String.Format(@"(%{0}{0})", hex);
    10.             string unreserved = String.Format(@"({0}|{1}|{2}|{3})", alpha, digit, safe, extra);
    11.             string uchar = String.Format(@"({0}|{1})", unreserved, escape);
    12.             string reserved = @"(;|/|\?|:|@|&|=)";
    13.             string xchar = String.Format(@"({0}|{1}|{2})", unreserved, reserved, escape);
    14.             string digits = String.Format(@"({0}+)", digit);

    15.             string alphadigit = String.Format(@"({0}|{1})", alpha, digit);
    16.             string domainlabel = String.Format(@"({0}|{0}({0}|-)*{0})", alphadigit);
    17.             string toplabel = String.Format(@"({0}|{0}({1}|-)*{1})", alpha, alphadigit);
    18.             string hostname = String.Format(@"(({0}\.)*{1})", domainlabel, toplabel);
    19.             string hostnumber = String.Format(@"{0}\.{0}\.{0}\.{0}", digits);
    20.             string host = String.Format(@"({0}|{1})", hostname, hostnumber);
    21.             string port = digits;
    22.             string hostport = String.Format(@"({0}(:{1}){{0,1}})", host, port);
    23.             string hsegment = String.Format(@"(({0}|;|:|@|&|=)*)", uchar);
    24.             string search = String.Format(@"(({0}|;|:|@|&|=)*)", uchar);
    25.             string hpath = String.Format(@"{0}(/{0})*", hsegment);
    26.             string httpurl = String.Format(@"http://{0}(/{1}(\?{2}){{0,1}}){{0,1}}", hostport, hpath, search);

    27.             #endregion
    复制代码
    1. #region Ftp

    2.             string user = String.Format(@"(({0}|;|\?|&|=)*)", uchar);
    3.             string password = String.Format(@"(({0}|;|\?|&|=)*)", uchar);
    4.             string login = String.Format(@"(({0}(:{1}){{0,1}}@){{0,1}}{2})", user, password, hostport);
    5.             string fsegment = String.Format(@"(({0}|\?|:|@|&|=)*)", uchar);
    6.             string ftptype = @"(A|I|D|a|i|d)";
    7.             string fpath = String.Format(@"({0}(/{0})*)", fsegment);
    8.             string ftpurl = String.Format(@"ftp://{0}(/{1}(;type={2}){{0,1}}){{0,1}}", login, fpath, ftptype);

    9.             #endregion
    复制代码
    1. #region News

    2.             string group = String.Format(@"({0}({0}|{1}|-|\.|\+|_)*)", alpha, digit);
    3.             string article = String.Format(@"(({0}|;|/|\?|:|&|=)+@{1})", uchar, host);
    4.             string grouppart = String.Format(@"(\*|{0}|{1})", group, article);
    5.             string newsurl = String.Format(@"(news:{0})", grouppart);

    6.             #endregion
    复制代码
    1. #region Nntpurl

    2.             string nntpurl = String.Format(@"nntp://{0}/{1}(/{2}){{0,1}}", hostport, group, digits);

    3.             #endregion
    复制代码
    1. #region Telnet

    2.             string telneturl = String.Format(@"telnet://{0}/{{0,1}}", login);

    3.             #endregion
    复制代码
    1. #region Gopher

    2.             string gtype = xchar;
    3.             string selector = String.Format(@"({0}*)", xchar);
    4.             string gopherplus_string = String.Format(@"({0}*)", xchar);
    5.             string gopherurl = String.Format(@"gopher://{0}(/({1}({2}(%09{3}(%09{4}){{0,1}}){{0,1}}){{0,1}}){{0,1}}){{0,1}}", hostport, gtype, selector, search, gopherplus_string);

    6.             #endregion
    复制代码
    1. #region Wais

    2.             string database = String.Format(@"({0}*)", uchar);
    3.             string wtype = String.Format(@"({0}*)", uchar);
    4.             string wpath = String.Format(@"({0}*)", uchar);
    5.             string waisdatabase = String.Format(@"(wais://{0}/{1})", hostport, database);
    6.             string waisindex = String.Format(@"(wais://{0}/{1}\?{2})", hostport, database, search);
    7.             string waisdoc = String.Format(@"(wais://{0}/{1}/{2}/{3})", hostport, database, wtype, wpath);
    8.             string waisurl = String.Format(@"{0}|{1}|{2}", waisdatabase, waisindex, waisdoc);

    9.             #endregion
    复制代码
    1. #region Mailto

    2.             string encoded822addr = String.Format(@"({0}+)", xchar);
    3.             string mailtourl = String.Format(@"mailto:{0}", encoded822addr);

    4.             #endregion
    复制代码
    1. #region File

    2.             string fileurl = String.Format(@"file://({0}{{0,1}}|localhost)/{1}", host, fpath);

    3.             #endregion
    复制代码
    1. #region Prosperourl

    2.             string fieldname = String.Format(@"({0}|\?|:|@|&)", uchar);
    3.             string fieldvalue = String.Format(@"({0}|\?|:|@|&)", uchar);
    4.             string fieldspec = String.Format(@"(;{0}={1})", fieldname, fieldvalue);
    5.             string psegment = String.Format(@"(({0}|\?|:|@|&|=)*)", uchar);
    6.             string ppath = String.Format(@"({0}(/{0})*)", psegment);
    7.             string prosperourl = String.Format(@"prospero://{0}/{1}({2})*", hostport, ppath, fieldspec);

    8.             #endregion
    复制代码
    1. #region Otherurl

    2.             //otherurl equal genericurl
    3.             string urlpath = String.Format(@"(({0})*)", xchar);
    4.             string scheme = String.Format(@"(({0}|{1}|\+|-|\.)+)", lowalpha, digit);
    5.             string ip_schemepar = String.Format(@"(//{0}(/{1}){{0,1}})", login, urlpath);
    6.             string schemepart = String.Format(@"(({0})*|{1})", xchar, ip_schemepar);
    7.             string genericurl = String.Format(@"{0}:{1}", scheme, schemepart);
    8.             string otherurl = genericurl;

    9.             #endregion
    复制代码

    有了Pattern剩下的就简单多了,无非就是正则表达式的验证了,以Http为例:

    Http的pattern为string httpurl,假设要验证的Url为url,所以验证url的代码如下:

    1. Regex regex = new Regex(httpurl);
    2. bool isMatchHttp = regex.IsMatch(url);
    复制代码

    (文/pangxiaoliang[北京]流浪者

  • 相关阅读:
    Mac OS X开发学习 -打开文件选择器并获取文件
    Mac OS X开发学习
    第三方开源
    Xcode操作
    Mac操作
    XCode6.1 错误
    ios 本地推送
    IOS 语法
    IOS 其他
    IOS 语法
  • 原文地址:https://www.cnblogs.com/94cool/p/2244864.html
Copyright © 2020-2023  润新知