如何检查给定的字符串是否是有效的URL地址?

我对正则表达式的知识是基本的,不允许我从我已经在网上看到的数百个正则表达式中进行选择。


当前回答

Regardless the broad question asked, I post this for anyone in the future who is looking for something simple... as I think validating a URL has no perfect regular expression that fit all needs, it depends on your requirements, i.e: in my case, I just needed to verify if a URL is in the form of domain.extension and I wanted to allow the www or any other subdomain like blog.domain.extension I don't care about http(s) as in my app I have a field which says "enter the URL" so it's obvious what that entered string is.

这是regEx:

/^(www\.|[a-zA-Z0-9](.*[a-zA-Z0-9])?\.)?((?!www)[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9])\.[a-z]{2,5}(:[0-9]{1,5})?$/i

这个regExp中的第一个块是:

(www \ | [a-zA-Z0-9] (. * [a-zA-Z0-9]) ? \。)?——>我们开始检查URL是否以www开头。[a-zA-Z0-9]或[a-zA-Z0-9] (. *) ?这意味着一个字母或数字+ (anyCharacter(0或多次)+另一个字母或数字),然后是一个点

注意(.*[a-zA-Z0-9])?\.)?我们翻译由(anyCharacter(0或多次)+另一个字母或数字) 是可选的(可以是或不是)这就是为什么我们将它分组在括号之间,后面跟着问号?

到目前为止我们讨论的整个块也放在括号之间,后面跟着?这意味着WWW或任何其他词(表示子域)都是可选的。

第二部分是:((? ! www) [a-zA-Z0-9] [a-zA-Z0-9 -] + [a-zA-Z0-9]) \。——>表示“域”部分,它可以是任何单词(www除外),以字母或数字开头+任何其他字母(包括破折号“-”)重复一次或多次,以任何字母或数字结尾,后面跟一个点。

最后一部分是[a-z]{2,}——>,它代表“扩展名”,它可以是任何字母重复2次或更多次,所以它可以是com, net, org, art基本上任何扩展名

其他回答

我刚刚写了一篇博客文章,介绍了一个很好的解决方案,可以识别大多数常用格式的url,比如:

www.google.com http://www.google.com mailto: somebody@google.com somebody@google.com url www.url-with-querystring.com/ ? = has-querystring

使用的正则表达式是:

/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)/

我认为有些人无法使用您的php代码,因为其中隐含的修饰符。我复制了你的代码作为一个例子:

if(
    preg_match(
        "/^{$IRI_reference}$/iu",
        'http://www.url.com'
    )
){
    echo 'true';
}

注意“i”和“u”修饰语。如果没有“u”,PHP会抛出一个异常:

Warning: preg_match() [function.preg-match]: Compilation failed: character value in \x{...} sequence is too large at offset XX

下面是我从URL中提取不同部分的正则表达式:

^ ((? (?): http | | ws ftp) s ? | sftp ):\/\/?)?([^:/\ s .#?]+\.[^:/\ s / # ?] + | localhost) (d: \ +)?((?:\/\ w +)*\/)?([\ w \-.]+[^#?\ s ]+)?([^#]+)?(#[\ w - *) ?美元

((?: ?: http | ftp | ws) s ? | sftp): \ \ / ?) ?(组1):提取协议 ([^: / \ s .#?]+\.[^:/\ # ?] + | localhost)(组2):提取的主机名 (: \ d +) ?(组3):提取端口号 ((?): \ / \ w +)*\/)?([\ w \-.]+[^#?\ s] +) ?(组4和组5):提取路径部分 ([^ #] +) ?(组6):提取查询部分 (# (\ w -) *) ?(组7):提取哈希部分

对于上面列出的正则表达式的每个部分,您可以删除结尾?强制(或加1使其兼性)。你也可以删除正则表达式开头的^和结尾的$,这样它就不需要匹配整个字符串了。

请参阅regex101。

注意:这个正则表达式不是100%安全的,可能接受一些字符串,这些字符串不一定是有效的url,但它确实验证了一些标准。它的主要目标是提取URL的不同部分,而不是验证它。

我想我找到了一个更通用的regexp来验证url,特别是网站

​(https?:\/\/)?(www\.)[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)|(https?:\/\/)?(www\.)?(?!ww)[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)

它不允许例如www.something或http://www或http://www.something

点击这里查看:http://regexr.com/3e4a2

您没有指定使用哪种语言。 如果PHP是,有一个本地函数:

$url = 'http://www.yoururl.co.uk/sub1/sub2/?param=1&param2/';

if ( ! filter_var( $url, FILTER_VALIDATE_URL ) ) {
    // Wrong
}
else {
    // Valid
}

返回过滤后的数据,如果过滤失败则返回FALSE。

看看这里>>

希望能有所帮助。