Title |
Test
Find
Pattern Title
|
Expression |
^(((ht|f)tp(s?))\://)?((([a-zA-Z0-9_\-]{2,}\.)+[a-zA-Z]{2,})|((?:(?:25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)(?(\.?\d)\.)){4}))(:[a-zA-Z0-9]+)?(/[a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~]*)?$ |
Description |
Hopefully an all-encompassing expression to validate a URL. Supports an optional protocol, either a domain or IP address, an optional port number and an optional path. |
Matches |
this.com | https://this.com:8080/this/this.htm | ftp://255.255.255.255/ |
Non-Matches |
.this.com | https://this.com:/ | ftps://255.256.255.255/ |
Author |
Rating:
Tom Hartland
|
Source |
Combination of patterns from Brad Dobyns (protocol), Brandon Luhring (domain, port and path - slightly altered) and Jerry Schmersahl (IP numbers) |
Your Rating |
|
Title: Good Job Tom
Name: Ronit Passwala
Date: 7/2/2014 7:31:46 AM
Comment:
I got the exact solution for which I was looking. Awesome regexp submitted.
thank you,
Tom.
Title: That perl all-encompassing regex from perl doesn't catch port numbers
Name: Steve Webb
Date: 10/6/2009 10:45:51 PM
Comment:
Randal -
So, it's Regexp::Common, not Regex::Common, and it doesn't seem to catch a port number as part of the URL, so it's not really all-encompassing.
Title: Just a tad too restrictive
Name: Andrew Gail
Date: 6/15/2006 10:03:32 AM
Comment:
Tom, I'm very impressed with this implementation. It does everything I need it to with one exception:
http://localhost/ or http://intranetserver/ doesn't give a match.
I picked this up when testing it against some of our internal servers, this may not have been a requirement for you however.
It is still a good base for me to start tho :)
Title: Constructive Critism
Name: Tom Hartland
Date: 4/14/2005 10:55:59 AM
Comment:
Randal, I'm open to constructive critism, so if you'd like to provide some I might take note of your comments.
The RegEx you have given does not achieve what I set out to make my RegEx achieve.
If you actually have a RegEx which provides the functionality that I stated that mine "hopefully" does, then I'd be interested to see it.
Title: Not all encompassing
Name: Randal L. Schwartz
Date: 4/5/2005 11:48:13 AM
Comment:
The all encompassing regex for an HTTP URI is given by
perl -MRegex::Common -le 'print $RE{URI}{HTTP}'
and is
(?:(?:http)://(?:(?:(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*(?:[a-zA-Z][-a-zA-Z0-9]*[a-zA-Z0-9]|[a-zA-Z])[.]?)|(?:[0-9]+[.][0-9]+[.][0-9]+[.][0-9]+)))(?::(?:(?:[0-9]*)))?(?:/(?:(?:(?:(?:(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*)(?:/(?:(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)(?:;(?:(?:[a-zA-Z0-9\-_.!~*'():@&=+$,]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*))*))*))(?:[?](?:(?:(?:[;/?:@&=+$,a-zA-Z0-9\-_.!~*'()]+|(?:%[a-fA-F0-9][a-fA-F0-9]))*)))?))?)