Re: regex puzzle




/^((http|ftp):\/\/)?.+$/ In my opinion the problem is that ' .+' means 'anything', and ''" is matched by this part of the regex ("" is anything too).

regards
Pedro

----- Original Message ----- From: "Roberto Ruiz" <rruiz@xxxxxxxxxxxxxxxx>
To: <beginners@xxxxxxxx>
Sent: Friday, July 29, 2005 6:52 AM
Subject: Re: regex puzzle




On Fri, Jul 29, 2005 at 01:48:15PM +0800, bingfeng zhao wrote:
See following sample code:

my @address = ("http://test";, "http://";, "www", "", "ftp:/foo" );
for (@address)
{
print "\"$_\" passed! \n" if /^((http|ftp):\/\/)?.+$/;
                         #          ^               ^
                         # these parentesis are making an atom of the
  # enclosed part of the regex
}
why "http://"; and "ftp:/foo" can pass the check?

Because the () atomize the first part of your regex and then the ? is asking for 0 or 1 of that atom.

Droping the ()?:       /^(http|ftp):\/\/.+$/

Or, more redable:      m!^(http|ftp)://.+$!

And a little shorter:  m!^(ht|f)tp://.+$!

HTH, Roberto Ruiz


-- To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx For additional commands, e-mail: beginners-help@xxxxxxxx <http://learn.perl.org/> <http://learn.perl.org/first-response>



.



Relevant Pages

  • RE: [PHP] Need help with RegEx
    ... Download and play with "The Regex Coach" ... I want what is inside the tags. ... fifth atom = all of the rest of the source html after the ...
    (php.general)
  • Re: regex puzzle
    ... Roberto Ruiz wrote: ... # these parentesis are making an atom of the ... # enclosed part of the regex ... asking for 0 or 1 of that atom. ...
    (perl.beginners)
  • Re: Parse a filename (this SHOULD be easy, right?)
    ... > that needs to be sprayed with a regex. ... I prefer his regular expession ... Regards, ... Prev by Date: ...
    (comp.lang.perl.misc)
  • Re: Ada 2005?
    ... You are right and I did see my error and correted it by translating the ... regex to plain english. ... With Regards ... Martin ...
    (comp.lang.ada)
  • Re: Need help with a regular expression!
    ... You need to use this RegEx ... Regards ... Sachin Korgaonkar ... The map files looks like this: ...
    (microsoft.public.dotnet.framework.aspnet)