URL validation regex allows one case I'd like to disallow
Here is my regex:
^(?#Protocol)((ht)tp(s?)\:\/\/|~\/|\/){1}((?#IP Address)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?#Subdomains)(([-\w]+\.)+(?#TopLevel Domains))([a-z]+))(?#Port)(:[\d]{1,5})?(\/\S*)*$
It allows this URL, but I'd like it to disallow http://1开发者_开发百科0.10.10.10.comasdf
.
Can anyone help me understand where it is wrong?
BTW, the following URLs are (correctly) allowed by the regex:
http://www.google.com
http://google.com
http://www.google.COM
http://google.co
http://www.google.c
http://www.google.biz
http://www.google.edu
http://android.google.com
http://www.google.com.asdf
http://android.google.com.jp
http://www.google.michael
http://www.mysite10.com
http://10mysite.com
http://209.85.148.103
http://209.85.148.103:8080
http://www.google.com:8080
http://www.google.japan
http://209.85.148.103/asdf
http://www.google.japan/asdf
http://www.google.japan/asdf/xcvb
http://209.85.148.103/asdf/xcvb
http://www.google.japan/asdf10/xcvb
http://209.85.148.103/asdf10/xcvb
http://www.google.japan/asdf10/xcvb30grts
http://209.85.148.103/asdf10/xcvb30grts
http://209.85.148.103/asdf10#xcvb30grts
http://www.google.japan/asdf10#xcvb30grts
When you ip address doesn't match, it tries to match the |
condition. And it successfully does because 10.10.10.10.comasdf
matches ([-\w]+\.)+([a-z]+)
Try puting a ?>
before the (?#IP Address)
. That way, when the ip address does match, it won't attempt the |(?#Subdomains)(([-\w]+\.)+(?#TopLevel Domains))([a-z]+)
after failing the (?#Port)(:[\d]{1,5})?(\/\S*)*$
^(?#Protocol)((ht)tp(s?)\:\/\/|~\/|\/){1}(?>(?#IP Address)(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?#Subdomains)(([-\w]+\.)+(?#TopLevel Domains))([a-z]+))(?#Port)(:[\d]{1,5})?(\/\S*)*$
See: http://regexr.com?2u8ks
精彩评论