如何检查给定的字符串是否是有效的URL地址?
我对正则表达式的了解很基础,并不能让我从已经在网上看到的成百上千个正则表达式中进行选择。
如何检查给定的字符串是否是有效的URL地址?
我对正则表达式的了解很基础,并不能让我从已经在网上看到的成百上千个正则表达式中进行选择。
对我来说,最好的URL正则表达式是:
"(([\w]+:)?//)?(([\d\w]|%[a-fA-F\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?"
(([\\w]+:)?//)?(([\\d\\w]|%[a-fA-f\\d]{2,2})+(:([\\d\\w]|%[a-fA-f\\d]{2,2})+)?@)?([\\d\\w][-\\d\\w]{0,253}[\\d\\w]\\.)+[\\w]{2,4}(:[\\d]+)?(/([-+_~.\\d\\w]|%[a-fA-f\\d]{2,2})*)*(\\?(&?([-+_~.\\d\\w]|%[a-fA-f\\d]{2,2})=?)*)?(#([-+_~.\\d\\w]|%[a-fA-f\\d]{2,2})*)?
- James Kuang\w
替换为\p{L}
。 - ndm13这里有一个良好的规则适用于所有可能情况:端口、参数等等。
/(https?:\/\/(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{0,61}[a-z0-9])(:?\d*)\/?([a-z_\/0-9\-#.]*)\??([a-z_\/0-9\-#=&]*)/g
https://www.domainname.com http://www.domainname.com http://domainname.com https://domainname.com https://www.domainname.com/ http://www.domainname.com/ http://domainname.com/ https://domainname.com/ https://www.domainname.com/inner-page http://www.domainname.com/inner-page http://domainname.com/inner-page https://domainname.com/inner-page https://www.domainname.com/inner-page/ http://www.domainname.com/inner-page/ http://domainname.com/inner-page/ https://domainname.com/inner-page/
- ApsaraAruna我写了一个小的Groovy版本,可以运行
它匹配以下URL(对我来说足够好了)
public static void main(args) {
String url = "go to http://www.m.abut.ly/abc its awesome"
url = url.replaceAll(/https?:\/\/w{0,3}\w*?\.(\w*?\.)?\w{2,3}\S*|www\.(\w*?\.)?\w*?\.\w{2,3}\S*|(\w*?\.)?\w*?\.\w{2,3}[\/\?]\S*/ , { it ->
"woof${it}woof"
})
println url
}
http://google.com
http://google.com/help.php
http://google.com/help.php?a=5
http://www.google.com
http://www.google.com/help.php
http://www.google.com?a=5
google.com?a=5
google.com/help.php
google.com/help.php?a=5
http://www.m.google.com/help.php?a=5 (and all its permutations)
www.m.google.com/help.php?a=5 (and all its permutations)
m.google.com/help.php?a=5 (and all its permutations)
对于不以http
或www
开头的任何URL,重要的是它们必须包含/
或?
。
我敢说这可以再微调一下,但它非常简短紧凑,已经做得很好了...因为你基本上可以将其分成三个部分:
查找任何以http
开头的内容:
https?:\/\/w{0,3}\w*?\.\w{2,3}\S*
寻找以www
开头的任何内容:
www\.\w*?\.\w{2,3}\S*
或者找到任何必须以文本开头,然后是一个点,至少有2个字母,然后是一个?
或 /
的内容:
\w*?\.\w{2,3}[\/\?]\S*
https://http://www.google.com
。 - undefined我无法找到我要寻找的正则表达式,所以我修改了一个正则表达式来满足我的要求,现在看起来似乎工作得很好。我的要求是:
这是我想出来的东西,欢迎提出建议:
@Test
public void testWebsiteUrl(){
String regularExpression = "((http|ftp|https):\\/\\/)?[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-\\.,@?^=%&:/~\\+#]*[\\w\\-\\@?^=%&/~\\+#])?";
assertTrue("www.google.com".matches(regularExpression));
assertTrue("www.google.co.uk".matches(regularExpression));
assertTrue("http://www.google.com".matches(regularExpression));
assertTrue("http://www.google.co.uk".matches(regularExpression));
assertTrue("https://www.google.com".matches(regularExpression));
assertTrue("https://www.google.co.uk".matches(regularExpression));
assertTrue("google.com".matches(regularExpression));
assertTrue("google.co.uk".matches(regularExpression));
assertTrue("google.mu".matches(regularExpression));
assertTrue("mes.intnet.mu".matches(regularExpression));
assertTrue("cse.uom.ac.mu".matches(regularExpression));
assertTrue("http://www.google.com/path".matches(regularExpression));
assertTrue("http://subdomain.web-site.com/cgi-bin/perl.cgi?key1=value1&key2=value2e".matches(regularExpression));
assertTrue("http://www.google.com/?queryparam=123".matches(regularExpression));
assertTrue("http://www.google.com/path?queryparam=123".matches(regularExpression));
assertFalse("www..dr.google".matches(regularExpression));
assertFalse("www:google.com".matches(regularExpression));
assertFalse("https://www@.google.com".matches(regularExpression));
assertFalse("https://www.google.com\"".matches(regularExpression));
assertFalse("https://www.google.com'".matches(regularExpression));
assertFalse("http://www.google.com/path'".matches(regularExpression));
assertFalse("http://subdomain.web-site.com/cgi-bin/perl.cgi?key1=value1&key2=value2e'".matches(regularExpression));
assertFalse("http://www.google.com/?queryparam=123'".matches(regularExpression));
assertFalse("http://www.google.com/path?queryparam=12'3".matches(regularExpression));
}
http/stackoverflow.com/
、h77ps://stackoverflow.com/
和 //stackoverflow.com/
。 - TiberiumFusion function validateURL(textval) {
var urlregex = new RegExp(
"^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
return urlregex.test(textval);
}
匹配 http://site.com/dir/file.php?var=moo | ftp://user:pass@site.com:21/file/dir
不匹配 site.com | http://site.com/dir//
http://www.goo gle.com
将匹配。 - Ifch0o1function validateURL(textval) {
var urlregex = new RegExp(
"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$");
return urlregex.test(textval);
}
匹配项 http://www.asdah.com/~joe | ftp://ftp.asdah.co.uk:2828/asdah%20asdah.gif | https://asdah.gov/asdh-ah.as
^(http|https):\/\/+[\www\d]+\.[\w]+(\/[\w\d]+)?
^((?:(?:http | ftp | ws)s? | sftp):\/\/?)?([^:/\s.#?]+\.[^:/\s#?]+ | localhost)(:\d+)?((?:\/\w+)*\/)?([\w\-.]+[^#?\s]+)?([^#]+)?(#[\w-]*)?$
((?:(?:http | ftp | ws)s? | sftp):\/\/?)?
(第1组):提取协议([^:/\s.#?]+\.[^:/\s#?]+ | localhost)
(第2组):提取主机名(:\d+)?
(第3组):提取端口号((?:\/\w+)*\/)?([\w\-.]+[^#?\s]+)?
(第4和第5组):提取路径部分([^#]+)?
(第6组):提取查询部分(#[\w-]*)?
(第7组):提取哈希部分
对于上面列出的每个正则表达式部分,您可以删除末尾的?
以强制匹配(或添加一个以使其变成可选项)。 您也可以删除正则表达式开头的 ^
和结尾的 $
,以便不必匹配整个字符串。
在此处查看。