用于验证URL的REGEX [英] REGEX for validating URL

查看:91
本文介绍了用于验证URL的REGEX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要正则表达式来验证以下形式的URL



 http://www.site.com 
https://www.site.com
http://site.com
https://site.com
http://domain.site.com
https ://domain.site.com
http://www.domain.site.com
https://www.domain.site.com
site.com
domain .site.com
http://www.site.com/path/to/dir/
https://www.site.com/path/to/dir/
http: //site.com/path/to/dir/
https://site.com/path/to/dir/
http://domain.site.com/path/to/dir/
https://domain.site.com/path/to/dir/
http://www.domain.site.com/path/to/dir/
https:// www.domain.domain.site.com/path/to/dir/
site.com/path/to/dir/
domain.site.com/path/to/dir/
http://www.site.com/path/to/file.html
https://www.site.com/path/to/file.html
http://site.com/ path / to / file.html
https://site.com/path/to/file.html
http://domain.site.com/path/to/file.html
https://domain.site.com/path/to/file.html
http://www.domain.site.com/path/to/file.html
https://www.domain.domain.site.com/path/to/file.html
site .com / path / to / file.html
domain.site.com/path/to/file.html



和相对路径

 ./ path / to / file.html 
./path/to/dir/
./path/to/dir
path / to /file.html
path / to / dir /
path / to / dir



(ftp://不允许)



文件扩展名可能是html,php,gif,jpg,png。



凭借我对正则表达式的了解,这将是我需要一年时间才能完成(如果不是更长时间)。昨天花了我一个多小时来做一个相对URL的正则表达式,并没有达到我想要的方式!我觉得因为不了解REGEX而需要道歉! :(



请注意,如果URL没有真正指向任何地方,这不是问题,我主要担心的是格式。我只需要格式为在那些(并且只有那些)示例中显示(这是一个详尽的列表)。只有那些是我将要使用和需要的,但如果它验证不同的格式它的确定(只要它只有http / https ...不允许使用ftp,ftps或其他任何内容。

解决方案

试试这个:

(^ [http [秒]:???//)([W] {3} [])([A-Z0-9] + [])+ COM(((/ [A-Z0-9] +)*( / [A-Z0-9] + /))*([A-Z0-9] +(HTML [。] | PHP | GIF | PNG)))

)?| (^(/)((([A-Z0-9] +)/)+ [。] |?(([A-Z0-9] +)/)+([A-Z0-9] + [ ](HTML | PHP | GIF | PNG)?))


I need regex that would validate the following forms of URL

http://www.site.com
https://www.site.com
http://site.com
https://site.com
http://domain.site.com
https://domain.site.com
http://www.domain.site.com
https://www.domain.site.com
site.com 
domain.site.com
http://www.site.com/path/to/dir/
https://www.site.com/path/to/dir/
http://site.com/path/to/dir/
https://site.com/path/to/dir/
http://domain.site.com/path/to/dir/
https://domain.site.com/path/to/dir/
http://www.domain.site.com/path/to/dir/
https://www.domain.domain.site.com/path/to/dir/
site.com/path/to/dir/
domain.site.com/path/to/dir/
http://www.site.com/path/to/file.html
https://www.site.com/path/to/file.html
http://site.com/path/to/file.html
https://site.com/path/to/file.html
http://domain.site.com/path/to/file.html
https://domain.site.com/path/to/file.html
http://www.domain.site.com/path/to/file.html
https://www.domain.domain.site.com/path/to/file.html
site.com/path/to/file.html
domain.site.com/path/to/file.html


And relative paths

./path/to/file.html
./path/to/dir/
./path/to/dir
path/to/file.html
path/to/dir/
path/to/dir


(ftp:// NOT permitted)

The file extension may be html, php, gif, jpg, png.

With my knowledge of regex this would take me a year to accomplish (if not longer). Took me more then an hour yesterday to do a regex for relative URL and that didn't turn out the way I want it to! I feel like I need to apologize for not knowing REGEX! :(

Just to note, it's not a problem if the URL doesn't really point anywhere, my main concern is the format. I just need the format to be in those (and only those) as the examples show (it's an exhaustive list). Only those is what I would be using and need, but if it validates a different format its ok (as long as its only http / https... ftp, ftps or anything else is NOT permitted).

解决方案

Try this:

(^(http[s]?://)?([w]{3}[.])?([a-z0-9]+[.])+com(((/[a-z0-9]+)*(/[a-z0-9]+/))*([a-z0-9]+[.](html|php|gif|png))?)


)|(^([.]/)?((([a-z0-9]+)/?)+|(([a-z0-9]+)/)+([a-z0-9]+[.](html|php|gif|png)))?


)


这篇关于用于验证URL的REGEX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆