Re: regex puzzle
- From: rruiz@xxxxxxxxxxxxxxxx (Roberto Ruiz)
- Date: Fri, 29 Jul 2005 03:52:26 -0600
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
.
- Follow-Ups:
- Re: regex puzzle
- From: Pedro Henrique Calais
- Re: regex puzzle
- Prev by Date: Want code to display date in different timezone
- Next by Date: Read a single line in a file.
- Previous by thread: Re: regex puzzle
- Next by thread: Re: regex puzzle
- Index(es):
Relevant Pages
|