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]
<http://learn.perl.org/> <http://learn.perl.org/first-response>