RegEx for URL

Instructional HowTos, posted by users. No questions here please.

Moderators: AmigoJack, helios, bbadmin, Bob Hansen, MudGuard

Post Reply
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

RegEx for URL

Post by Bob Hansen »

It would not be right not to share this........

If you have been looking for a RegEx for a URL, I found this one tonight at http://internet.ls-la.net/folklore/url-regexpr.html
I suspect the originator might really be ben_josephs. If not, he is still the one to help explain this to you if you have trouble.

====================================

(?:http://(?:(?:(?:(?:(?:[a-zA-Z\d](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?)\. )*(?:[a-zA-Z](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?))|(?:(?:\d+)(?:\.(?:\d+) ){3}))(?::(?:\d+))?)(?:/(?:(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F \d]{2}))|[;:@&=])*)(?:/(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{ 2}))|[;:@&=])*))*)(?:\?(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{ 2}))|[;:@&=])*))?)?)|(?:ftp://(?:(?:(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(? :%[a-fA-F\d]{2}))|[;?&=])*)(?::(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a- fA-F\d]{2}))|[;?&=])*))?@)?(?:(?:(?:(?:(?:[a-zA-Z\d](?:(?:[a-zA-Z\d]|- )*[a-zA-Z\d])?)\.)*(?:[a-zA-Z](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?))|(?:(?:\d+)(?:\.(?:\d+)){3}))(?::(?:\d+))?))(?:/(?:(?:(?:(?:[a-zA-Z\d$\-_.+! *'(),]|(?:%[a-fA-F\d]{2}))|[?:@&=])*)(?:/(?:(?:(?:[a-zA-Z\d$\-_.+!*'() ,]|(?:%[a-fA-F\d]{2}))|[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*))*)?)))?)|(?:/(?:(?:(?:(?:(?:[a-zA-Z\d\$\-_.!~*'(),])|(?:%[a-fA-F\d]{2})|[:@&=+])*)(?:/(?:(?:(?:[a-zA-Z\d\$\ -_.!~*'(),])|(?:%[a-fA-F\d]{2})|[:@&=+])*))*)?))|(?:(?:(?:(?:(?:[a-zA- Z\d\$\-_.!~*'(),])|(?:%[a-fA-F\d]{2})|[:@&=+])*)(?:/(?:(?:(?:[a-zA-Z\d \$\-_.!~*'(),])|(?:%[a-fA-F\d]{2})|[:@&=+])*))*)?)))

==============================================

And here is a link to Background information and documentation
Hope this was helpful.............good luck,
Bob
ben_josephs
Posts: 2456
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Thank you for that!

No, it's nothing to do with me.

Note that it uses the construct (?:, which is not supported by TextPad. And it appears to have gained some unwanted spaces in the process of being word-wrapped. Perhaps you'd like to tinker with it and try it out in WildEdit... :-)
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

This was just a different way of saying thanks to you for your support.
A humorous tribute as it is.

Have a Happy Thanksgiving.
Hope this was helpful.............good luck,
Bob
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

If I don't overlook something, it is not complete - https is not supported ;-)
(which could be cured by adding s? after the http at the beginning.)
Same for ftps:

It also does not support IDN.

And it does not limit domain names to 64 characters.
And it does not limit tlds to 2, 3, 4 or 6 characters (although a list of valid tlds would be even better, a restriction on the length would help ...)

And it allows nonsense IP addresses (e.g. 123456.123456.123456.123456)

etc.

So this expression might match all correct non-https-URLs, it also matches on many non-URLs.
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

:idea: Re the missing protocols:

:twisted: Try replacing the front end with this:

Code: Select all

(?:[ftp|sftp|https*]://


=============================================
I guess it's a good thing that it seems to be using POSIX on.
If not, it would really be difficult to analyze. :shock:
Hope this was helpful.............good luck,
Bob
User avatar
MudGuard
Posts: 1295
Joined: Sun Mar 02, 2003 10:15 pm
Location: Munich, Germany
Contact:

Post by MudGuard »

(?:[ftp|sftp|https*]://

would definitely not work.

As I already stated: s? at the two appropriate places would be enough.

[ftp|sftp|https*]
allows one of the characters f, t, p, |, s, h and * ...

ftp is already in the Regex.

You probably wanted () instead of [] - but even then:
ftp and http allow different urls - ftp allows username:password@ before the host, http(s) forbids username and password.

And https* would allow httpsssssssssssssss ;-)
User avatar
Bob Hansen
Posts: 1517
Joined: Sun Mar 02, 2003 8:15 pm
Location: Salem, NH
Contact:

Post by Bob Hansen »

Thanks MudGuard. I defer to your solution on this.

Would like to continue the joke, but must beg off........ The break from the serious work has been good, but gotta get back to work.......

Good luck if you wish to continue with the final solution. It's good to see a sense of humor still exists here on the forum.
Hope this was helpful.............good luck,
Bob
ben_josephs
Posts: 2456
Joined: Sun Mar 02, 2003 9:22 pm

Post by ben_josephs »

Jokes? You want jokes?

$Old_MacDonald = q#print #;
$had_a_farm = (q-q:Just another Perl hacker,:-);
s/^/q[Sing it, boys and girls...],$Old_MacDonald.$had_a_farm/eieio;
Post Reply