Regex URL的正则表达式显示“URL”;。“金融”;无效/错误
我为我的laravel URL验证器得到了这个正则表达式,如果我放了一个“.finance”域,它表明它与正则表达式相反。怎么了?到目前为止,所有其他经过测试的域结尾都能正常工作Regex URL的正则表达式显示“URL”;。“金融”;无效/错误,regex,laravel,Regex,Laravel,我为我的laravel URL验证器得到了这个正则表达式,如果我放了一个“.finance”域,它表明它与正则表达式相反。怎么了?到目前为止,所有其他经过测试的域结尾都能正常工作 $regex = '/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/'; 将[a-z]{2,5}替换为[a-z]{2,5},
$regex = '/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/';
将
[a-z]{2,5}
替换为[a-z]{2,5}
,以允许TLD中有任意两个或多个字母
删除{1}
,这些总是不必要的
(http:\/\/www\.\124; https:\/\/www\.\124; http:\/\/\\\/\\\/\\/)?
是一种重复的方式,使用可选组/字符将其缩短为(?:https?:\/\/(?:www\)?
使用
/^(?:https?:\/\/(?:www\)?[a-z0-9]+(?:[-.][a-z0-9]+)*\.[a-z]{2,}(?:[0-9]{1,5})(\/*)$/
看
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
http 'http'
--------------------------------------------------------------------------------
s? 's' (optional (matching the most amount
possible))
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
www 'www'
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
[a-z0-9]+ any character of: 'a' to 'z', '0' to '9'
(1 or more times (matching the most amount
possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
[-.] any character of: '-', '.'
--------------------------------------------------------------------------------
[a-z0-9]+ any character of: 'a' to 'z', '0' to '9'
(1 or more times (matching the most
amount possible))
--------------------------------------------------------------------------------
)* end of grouping
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
[a-z]{2,} any character of: 'a' to 'z' (at least 2
times (matching the most amount possible))
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
[0-9]{1,5} any character of: '0' to '9' (between 1
and 5 times (matching the most amount
possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
( group and capture to \1 (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\/ '/'
--------------------------------------------------------------------------------
.* any character except \n (0 or more times
(matching the most amount possible))
--------------------------------------------------------------------------------
)? end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
在
{2,5}
finance
中增加5个字符是7个字符非常感谢!