Regex 用于URL验证的正则表达式

Regex 用于URL验证的正则表达式,regex,Regex,我有一个可能是URL的字符串。我想通过正则表达式确定它是否是Http/FTP URL。如果字符串是有效的URL,那么我将使其成为超链接和蓝色等,如果它不是基于正则表达式的真或假的URL,我想进一步决定 什么是最好的正则表达式 这段perl代码非常准确,可能并不完美。组1包含http或ftp if ($str =~ /^(http|ftp):\/\/[\w.%@]+(:?[?].*)?/) { print "matches $1\n"; } else { print "no mat

我有一个可能是URL的字符串。我想通过正则表达式确定它是否是Http/FTP URL。如果字符串是有效的URL,那么我将使其成为超链接和蓝色等,如果它不是基于正则表达式的真或假的URL,我想进一步决定


什么是最好的正则表达式

这段perl代码非常准确,可能并不完美。组1包含
http
ftp

if ($str =~ /^(http|ftp):\/\/[\w.%@]+(:?[?].*)?/) {
    print "matches $1\n";
} else {
    print "no match\n";
}

以防您想知道url是否真的存在:

function url_exist($url){//se passar a URL existe
    $c=curl_init();
    curl_setopt($c,CURLOPT_URL,$url);
    curl_setopt($c,CURLOPT_HEADER,1);//get the header
    curl_setopt($c,CURLOPT_NOBODY,1);//and *only* get the header
    curl_setopt($c,CURLOPT_RETURNTRANSFER,1);//get the response as a string from curl_exec(), rather than echoing it
    curl_setopt($c,CURLOPT_FRESH_CONNECT,1);//don't use a cached version of the url
    if(!curl_exec($c)){
        //echo $url.' inexists';
        return false;
    }else{
        //echo $url.' exists';
        return true;
    }
    //$httpcode=curl_getinfo($c,CURLINFO_HTTP_CODE);
    //return ($httpcode<400);
}
函数url\u exist($url){//se passar a url existe
$c=curl_init();
curl_setopt($c,CURLOPT_URL,$URL);
curl_setopt($c,CURLOPT_HEADER,1);//获取头
curl_setopt($c,CURLOPT_NOBODY,1);//和*仅*获取标题
curl_setopt($c,CURLOPT_RETURNTRANSFER,1);//从curl_exec()获取字符串形式的响应,而不是回显它
curl_setopt($c,CURLOPT_FRESH_CONNECT,1);//不要使用url的缓存版本
如果(!curl_exec($c)){
//echo$url.“不精确列表”;
返回false;
}否则{
//echo$url.“存在”;
返回true;
}
//$httpcode=curl\u getinfo($c,CURLINFO\u HTTP\u代码);

//return($HttpCode)可能重复您在谷歌搜索过“URL正则表达式”吗?什么风格的regexp/用于哪种语言?可能重复该函数需要多长时间?