Subject: Re: [nycphp-talk] regexp for URLs (is this correct?)
Phillip Powell
phillip.powell at adnet-sys.com
Mon May 3 14:57:03 EDT 2004
I would think to simply support "https" all you would have to do is
change "http://" to ((?:https)|(?:http://...))
Then again, it's pretty beastly as it is! If that's not a call for a
global included variable, what is?
Phil
James B. Wetterau Jr. wrote:
> Jayesh Sheth says:
> > Hello all,
> >
> > I came up with this Perl-style regular expression to validate URLs.
> ...
> > Any suggestions or corrections?
> ...
>
> A Perl expert of some repute did the hard work of reading all the
> relevant protocol specs, BNF notation, etc., and came up with this
> valid URL parsing regexp. Anything shorter is likely to miss some
> special case.
>
> This is an article about how to do this:
> http://www.foad.org/~abigail/Perl/url2.html
> (Note that there were some judgement calls involved in interpretation
> of some of the edge cases.)
>
> This is the URL for the actual regexp:
> http://www.foad.org/~abigail/Perl/url3.regex
>
> This is the regexp (note that it is broken up into many lines but
> actually needs to be
> one long line; note also that it does not support https):
>
> (?: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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;[Ss][Ee][Cc][Tt][Ii][Oo][Nn
> ]=(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{2}))|[&=~:@/])+)))?))
> )?)|(?:nfs:(?:(?://(?:(?:(?:(?:(?:[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})|[:@&=+])*)(?:/(?:(?:(?:[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})|[:@&=+])*))*)?)))
>
>
>
>
>
>
>
> _______________________________________________
> talk mailing list
> talk at lists.nyphp.org
> http://lists.nyphp.org/mailman/listinfo/talk
>
--
---------------------------------------------------------------------------------
Phil Powell
Multimedia Programmer
ADNET Systems., Inc.
11260 Roger Bacon Drive Suite 403
Reston, VA 20191
#: (703) 709-7218 x107
Fax: (703) 709-7219
More information about the talk
mailing list