URL正则表达式大全

时间:2024-03-06 11:51:33

在没有懂得RFC1738的时辰,一向认为Url的正则表达式很简单,没想到Url有这么多分类,更没想到一个通俗的http的正则表达式也不是那么简单。
以下是我搜到的关于http的正则表达式:

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

当然这已经满足大项目组人的需求了,然则若是须要严格的验证的话还是要合适RFC1738了。

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

呵呵,空话不久不多说了,上代码

View Code
            string lowalpha = @"[a-z]";
 
            string hialpha = @"[A-Z]";
 
            string alpha = String.Format(@"({0}|{1})", lowalpha, hialpha);
 
            string digit = @"[0-9]";
 
            string safe = @"(\¥|-|_|\.|\+)";
 
            string extra = @"(!|\*|""|\(|\)|,)";
 
            string hex = String.Format(@"({0}|A|B|C|D|E|F|a|b|c|d|e|f)", digit);
 
            string escape = String.Format(@"(%{0}{0})", hex);
 
            string unreserved = String.Format(@"({0}|{1}|{2}|{3})", alpha, digit, safe, extra);
 
            string uchar = String.Format(@"({0}|{1})", unreserved, escape);
 
            string reserved = @"(;|/|\?|:|@|&|=)";
 
            string xchar = String.Format(@"({0}|{1}|{2})", unreserved, reserved, escape);
 
            string digits = String.Format(@"({0}+)", digit);
 

 
            string alphadigit = String.Format(@"({0}|{1})", alpha, digit);
 
            string domainlabel = String.Format(@"({0}|{0}({0}|-)*{0})", alphadigit);
 
            string toplabel = String.Format(@"({0}|{0}({1}|-)*{1})", alpha, alphadigit);
 
            string hostname = String.Format(@"(({0}\.)*{1})", domainlabel, toplabel);
 
            string hostnumber = String.Format(@"{0}\.{0}\.{0}\.{0}", digits);
 
            string host = String.Format(@"({0}|{1})", hostname, hostnumber);
 
            string port = digits;
 
            string hostport = String.Format(@"({0}(:{1}){{0,1}})", host, port);
 
            string hsegment = String.Format(@"(({0}|;|:|@|&|=)*)", uchar);
 
            string search = String.Format(@"(({0}|;|:|@|&|=)*)", uchar);
 
            string hpath = String.Format(@"{0}(/{0})*", hsegment);
 
            string httpurl = String.Format(@"http://{0}(/{1}(\?{2}){{0,1}}){{0,1}}", hostport, hpath, search);
View Code
 1             string user = String.Format(@"(({0}|;|\?|&|=)*)", uchar);
 2  
 3             string password = String.Format(@"(({0}|;|\?|&|=)*)", uchar);
 4  
 5             string login = String.Format(@"(({0}(:{1}){{0,1}}@){{0,1}}{2})", user, password, hostport);
 6  
 7             string fsegment = String.Format(@"(({0}|\?|:|@|&|=)*)", uchar);
 8 
 9             string ftptype = @"(A|I|D|a|i|d)";
10  
11             string fpath = String.Format(@"({0}(/{0})*)", fsegment);
12  
13             string ftpurl = String.Format(@"ftp://{0}(/{1}(;type={2}){{0,1}}){{0,1}}", login, fpath, ftptype);
View Code
 string group = String.Format(@"({0}({0}|{1}|-|\.|\+|_)*)", alpha, digit);
 
            string article = String.Format(@"(({0}|;|/|\?|:|&|=)+@{1})", uchar, host);
 
            string grouppart = String.Format(@"(\*|{0}|{1})", group, article);
 
            string newsurl = String.Format(@"(news:{0})", grouppart);
View Code
string nntpurl = String.Format(@"nntp://{0}/{1}(/{2}){{0,1}}", hostport, group, digits);
View Code
1  string telneturl = String.Format(@"telnet://{0}/{{0,1}}", login);
View Code
1             string gtype = xchar;
2  
3             string or = String.Format(@"({0}*)", xchar);
4  
5             string gopherplus_string = String.Format(@"({0}*)", xchar);
6  
7             string gopherurl = String.Format(@"gopher://{0}(/({1}({2}(%09{3}(%09{4}){{0,1}}){{0,1}}){{0,1}}){{0,1}}){{0,1}}", hostport, gtype, or, search, gopherplus_string);
View Code
 1             string database = String.Format(@"({0}*)", uchar);
 2  
 3             string wtype = String.Format(@"({0}*)", uchar);
 4  
 5             string wpath = String.Format(@"({0}*)", uchar);
 6  
 7             string waisdatabase = String.Format(@"(wais://{0}/{1})", hostport, database);
 8  
 9             string waisindex = String.Format(@"(wais://{0}/{1}\?{2})", hostport, database, search);
10  
11             string waisdoc = String.Format(@"(wais://{0}/{1}/{2}/{3})", hostport, database, wtype, wpath);
12  
13             string waisurl = String.Format(@"{0}|{1}|{2}", waisdatabase, waisindex, waisdoc);
View Code
1             string encoded822addr = String.Format(@"({0}+)", xchar);
2  
3             string mailtourl = String.Format(@"mailto:{0}", encoded822addr);

 

View Code
1 string fileurl = String.Format(@"file://({0}{{0,1}}|localhost)/{1}", host, fpath);
View Code
 1             string fieldname = String.Format(@"({0}|\?|:|@|&)", uchar);
 2  
 3             string fieldvalue = String.Format(@"({0}|\?|:|@|&)", uchar);
 4  
 5             string fieldspec = String.Format(@"(;{0}={1})", fieldname, fieldvalue);
 6  
 7             string psegment = String.Format(@"(({0}|\?|:|@|&|=)*)", uchar);
 8  
 9             string ppath = String.Format(@"({0}(/{0})*)", psegment);
10  
11             string prosperourl = String.Format(@"prospero://{0}/{1}({2})*", hostport, ppath, fieldspec);
12  
View Code
 1             string urlpath = String.Format(@"(({0})*)", xchar);
 2  
 3             string scheme = String.Format(@"(({0}|{1}|\+|-|\.)+)", lowalpha, digit);
 4  
 5             string ip_schemepar = String.Format(@"(//{0}(/{1}){{0,1}})", login, urlpath);
 6  
 7             string schemepart = String.Format(@"(({0})*|{1})", xchar, ip_schemepar);
 8  
 9             string genericurl = String.Format(@"{0}:{1}", scheme, schemepart);
10  
11             string otherurl = genericurl;

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

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

View Code
1 Regex regex = new Regex(httpurl);
2  
3 bool isMatchHttp = regex.IsMatch(url);