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

.



Relevant Pages

  • Re: Floats to chars and chars to floats
    ... Your sample code ... did not test for the appropriate macro that would indicate the ... existance of uint8_t. ... Prev by Date: ...
    (comp.lang.c)
  • export ado recordset to csv format
    ... I'm having trouble exporting or converting an ADO recordset to a comma ... separated (CSV) file. ... Does anyone have any sample code that works? ... Prev by Date: ...
    (microsoft.public.vb.database.ado)
  • RE: DB connectivity in word
    ... > How can I make database connectivity in Microsoft Word ... > VBE. ... If possible please give me one sample code. ... Prev by Date: ...
    (microsoft.public.word.vba.general)
  • Re: showInputDialog question
    ... for sample code. ... Canadian Mind Products, Roedy Green. ... http://mindprod.com Again taking new Java programming contracts. ... Prev by Date: ...
    (comp.lang.java.gui)
  • Re: DeviceIoControl (USB)
    ... >Since we have only these 2 lines of the sample code, ... >Those are dependent on the code using them, ... Remove NOSPAMFOR before emailing.) ... Prev by Date: ...
    (microsoft.public.vb.winapi)