Re: regex puzzle

2005-07-29 Thread Tom Allison

Roberto Ruiz wrote:

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





Nice shot!!

And yes, the ? is the culprit.
Wouldn't is be simpler if you just replaced .+$ with \w+ to make sure 
that there was something that matched.  Who cares if you match something 
 to the end of the line?  You also are accepting strings like:


http://`rm -rf /.`

m|(ht|f)tp://\w+|

Because you really would be expecting something similar to 'www' to follow?

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: regex puzzle

2005-07-29 Thread Pedro Henrique Calais


/^((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" <[EMAIL PROTECTED]>

To: 
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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>





--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: regex puzzle

2005-07-29 Thread Roberto Ruiz

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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: regex puzzle

2005-07-29 Thread Flemming Greve Skovengaard

bingfeng zhao wrote:

See following sample code:


use warnings;
use strict;
 
my @address = ("http://test";, "http://";, "www", "", "ftp:/foo" );
 
for (@address)

{
print "\"$_\" passed! \n" if /^((http|ftp):\/\/)?.+$/;
}
 


the running result is:
"http://test"; is valid.
"http://"; is valid.
"www" is valid.
"ftp:/foo" is valid.

why "http://"; and "ftp:/foo" can pass the check?
 


Because ((http|ftp):\/\/) is optional ( the ? following it does that ),
so any line with anything between the start and end of the line will pass.

--
Flemming Greve Skovengaard   FAITH, n.
a.k.a Greven, TuxPower   Belief without evidence in what is told
<[EMAIL PROTECTED]>  by one who speaks without knowledge,
4112.38 BogoMIPS of things without parallel.


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: regex puzzle

2005-07-29 Thread bingfeng zhao
 Oh, bug,it should be 

use warnings;
use strict;
 
my @address = ("http://test";, "http://";, "www", "", "ftp:/foo" );
 
for (@address)
{
print "\"$_\" is valid. \n" if /^((http|ftp):\/\/)?.+$/; 
}
 


|-Original Message-
|From: bingfeng zhao [mailto:[EMAIL PROTECTED] 
|Sent: Friday, July 29, 2005 1:48 PM
|To: Perl Beginners List
|Subject: regex puzzle
|
|See following sample code:
|
|
|use warnings;
|use strict;
| 
|my @address = ("http://test";, "http://";, "www", "", "ftp:/foo" );
| 
|for (@address)
|{
|print "\"$_\" passed! \n" if /^((http|ftp):\/\/)?.+$/; }  
|
|the running result is:
|"http://test"; is valid.
|"http://"; is valid.
|"www" is valid.
|"ftp:/foo" is valid.
|
|why "http://"; and "ftp:/foo" can pass the check?
| 
|


smime.p7s
Description: S/MIME cryptographic signature