简体   繁体   中英

Regular Expression to validate a URL or domain name.

Can someone please let me know what is wrong with my regular expression? I'm trying to just validate the beginning to URLs, mainly just host names (ie www.yahoo.com).

Regular Expression: ^(((ht|f)tp(s?))\:\/\/)?(www.)?([a-zA-Z0-9\-\.]{1,63})+\.([a-zA-Z]{2,5})$

Testing Values:

test.com – passes

test.c2om – fails 

test.test.com – passes

test.test.c2om – fails 

test.test.test.com – passes 

test.test.test.c2om – INVALID REGEX PATTERN 

This should return false, but instead returns nothing, both using javascript and c#… If you remove the {1,63} restriction on the size of the subdomain, it works…

You've created a catastrophic pattern - The engine will try to match ([a-zA-Z0-9\-\.]{1,63})+ in many ways until it fails. A simple solution is to remove {1,63} , as you've noted, it doesn't seem to be adding anything anyway.
Another option is to use the dots as anchors, so you cannot backtrack between them (this only gives you one way to match the text, and assumably, what you're trying to do):

([a-zA-Z0-9\-]{1,63}\.)*[a-zA-Z0-9\-]{1,63}

Keep in mind that isnt very correct anymore to assume all-ASCII-English letters in domain names. For example http://אתר.קום is a legal (and working) url.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM