如何检查给定的字符串是否是有效的URL地址?
我对正则表达式的知识是基本的,不允许我从我已经在网上看到的数百个正则表达式中进行选择。
如何检查给定的字符串是否是有效的URL地址?
我对正则表达式的知识是基本的,不允许我从我已经在网上看到的数百个正则表达式中进行选择。
当前回答
为了方便起见,这里有一个用于URL的一行regexp,它也将匹配localhost,其中您更可能有端口,而不是。com或类似的端口。
(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}(\.[a-z]{2,6}|:[0-9]{3,4})\b([-a-zA-Z0-9@:%_\+.~#?&\/\/=]*)
其他回答
如果你想采用更严格的规则,以下是我提出的:
isValidUrl(input) {
var regex = /^(((H|h)(T|t)(T|t)(P|p)(S|s)?):\/\/)?[-a-zA-Z0-9@:%._\+~#=]{2,100}\.[a-zA-Z]{2,10}(\/([-a-zA-Z0-9@:%_\+.~#?&//=]*))?/
return regex.test(input)
}
我试着制定我的url版本。我的需求是在一个字符串中捕获实例,其中可能的url可以是cse.uom.ac.mu -注意它的前面没有http或www
String regularExpression = "((((ht{2}ps?://)?)((w{3}\\.)?))?)[^.&&[a-zA-Z0-9]][a-zA-Z0-9.-]+[^.&&[a-zA-Z0-9]](\\.[a-zA-Z]{2,3})";
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));
//cannot contain 2 '.' after www
assertFalse("www..dr.google".matches(regularExpression));
//cannot contain 2 '.' just before com
assertFalse("www.dr.google..com".matches(regularExpression));
// to test case where url www must be followed with a '.'
assertFalse("www:google.com".matches(regularExpression));
// to test case where url www must be followed with a '.'
//assertFalse("http://wwwe.google.com".matches(regularExpression));
// to test case where www must be preceded with a '.'
assertFalse("https://www@.google.com".matches(regularExpression));
我写了一个很棒的版本,你可以运行
它匹配以下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*
或者找到任何必须有一个文本,然后一个点,然后至少两个字母,然后一个?或/:
\w*?\.\w{2,3}[\/\?]\S*
最好的正则表达式,我发现是:/ (^ | \ s) ((https ?:\/\/)?[\ w -) + (\ [\ w -]+)+\.?(:\ d +) ? (\ / \ S *) ?) / gi
ios迅速:(^ | \ \ s) ((https ?:\\/\\/)?[\\ w -]+(\\.[\\ w -]+)+\\.?(:\\ d +) ? (\ \ / \ \ S *) ?)
http://jsfiddle.net/9BYdp/1/
在这里找到
获取URL的部分(Regex)这篇文章讨论了解析URL以识别其各个组件。如果您想检查URL是否格式良好,它应该足以满足您的需求。
如果你需要检查它是否有效,你最终将不得不尝试访问另一端的任何东西。
不过,一般来说,使用框架或其他库提供的函数可能会更好。许多平台都包含了解析url的函数。例如,有Python的urlparse模块,在。net中你可以使用System模块。类的构造函数,作为验证URL的一种方法。