Rod
Rod

Reputation: 15457

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 (i.e. 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…

Upvotes: 2

Views: 1235

Answers (1)

Kobi
Kobi

Reputation: 138017

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.

Upvotes: 5

Related Questions