Should an empty regex match everything

Discussion in 'Perl Misc' started by Mintcake, Oct 8, 2007.

  1. Mintcake

    Mintcake Guest

    The script at the end of this post produces the folllowing output...
    Matching on 'plugh'
    Contains the word plugh
    Does not contain the word plugh
    Matching on 'xyzzy'
    Contains the word xyzzy
    Matching on ''
    1

    I don't understand why 3rd call to gettd() does not produce any
    matches.

    #!/usr/bin/env perl

    use strict;
    use warnings;

    my $tr = <<EOTR;
    <tr>
    <td>Contains the word plugh</td>
    <td>Does not contain the word plugh</td>
    <td>Contains the word xyzzy</td>
    </tr>
    EOTR

    sub gettd {
    my $regex = shift;
    print "Matching on '$regex'\n";
    for (grep /$regex/, $tr =~ /<td>(.*?)<\/td>/g) {
    print " $_\n";
    }
    }

    gettd('plugh');
    gettd('xyzzy');
    gettd('');

    my $regex = '';
    my $str = 'Any old string';
    print $str =~ /$regex/, "\n";
     
    Mintcake, Oct 8, 2007
    #1
    1. Advertising

  2. Mintcake <> writes:

    > I don't understand why 3rd call to gettd() does not produce any
    > matches.


    Because a pattern consisting of the empty string has a special
    meaning, documented in perlop:

    If the PATTERN evaluates to the empty string, the last success-
    fully matched regular expression is used instead. In this case,
    only the "g" and "c" flags on the empty pattern is honoured -
    the other flags are taken from the original pattern. If no
    match has previously succeeded, this will (silently) act
    instead as a genuine empty pattern (which will always match).

    //Makholm
     
    Peter Makholm, Oct 8, 2007
    #2
    1. Advertising

  3. >>>>> "Mintcake" == Mintcake <> writes:

    Mintcake> #!/usr/bin/env perl

    Beware this usage... if another user (such as the webserver) has a different
    PATH, they'll get a different Perl for this. Not good in the general case.

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Oct 8, 2007
    #3
  4. Mintcake

    Mintcake Guest

    On Oct 8, 12:25 pm, Peter Makholm <> wrote:
    > Mintcake <> writes:
    > > I don't understand why 3rd call to gettd() does not produce any
    > > matches.

    >
    > Because a pattern consisting of the empty string has a special
    > meaning, documented in perlop:
    >
    > If the PATTERN evaluates to the empty string, the last success-
    > fully matched regular expression is used instead. In this case,
    > only the "g" and "c" flags on the empty pattern is honoured -
    > the other flags are taken from the original pattern. If no
    > match has previously succeeded, this will (silently) act
    > instead as a genuine empty pattern (which will always match).
    >
    > //Makholm


    Thanks for that. I'm very glad to have it explained. However, I
    can't think of a scenario in which you would make use of this
    feature. Though of course, I'm sure there are plenty.
     
    Mintcake, Oct 9, 2007
    #4
  5. Mintcake

    Mintcake Guest

    On Oct 8, 7:09 pm, (Randal L. Schwartz) wrote:
    > >>>>> "Mintcake" == Mintcake <> writes:

    >
    > Mintcake> #!/usr/bin/env perl
    >
    > Beware this usage... if another user (such as the webserver) has a different
    > PATH, they'll get a different Perl for this. Not good in the general case.
    >
    > --
    > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    > <> <URL:http://www.stonehenge.com/merlyn/>
    > Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    > See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


    Good point. I'd seen this usage in another post and thought I'd try
    it out. I'd actually abandoned the idea since something like...

    #!/usr/bin/env perl -l

    .... doesn't work.

    I don't know how this idiom escaped into my example code without my
    permission. I'll be more careful next time.
     
    Mintcake, Oct 9, 2007
    #5
  6. On Tue, 09 Oct 2007 01:13:23 -0700, Mintcake <>
    wrote:

    >> If the PATTERN evaluates to the empty string, the last success-
    >> fully matched regular expression is used instead. In this case,
    >> only the "g" and "c" flags on the empty pattern is honoured -
    >> the other flags are taken from the original pattern. If no
    >> match has previously succeeded, this will (silently) act
    >> instead as a genuine empty pattern (which will always match).
    >>
    >> //Makholm

    >
    >Thanks for that. I'm very glad to have it explained. However, I
    >can't think of a scenario in which you would make use of this
    >feature. Though of course, I'm sure there are plenty.


    Actually I've never either used it nor felt a compelling need for it.
    To be fair I'm happy that in Perl 6 the "empty" match is being phased
    out.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Oct 9, 2007
    #6
  7. On Tue, 09 Oct 2007 01:16:56 -0700, Mintcake <>
    wrote:

    >> --
    >> Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    >> <> <URL:http://www.stonehenge.com/merlyn/>
    >> Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    >> See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


    Don't quote .sig's.

    >Good point. I'd seen this usage in another post and thought I'd try
    >it out. I'd actually abandoned the idea since something like...
    >
    >#!/usr/bin/env perl -l
    >
    >... doesn't work.
    >
    >I don't know how this idiom escaped into my example code without my
    >permission. I'll be more careful next time.


    It's controversial: /usr/bin/env was aimed at solving problems with
    variable locations of the actual perl interpreter. Eventually it turns
    out that it suffers from the same problem it should solve, but anyway
    there are people in both camps: those who deprecate it and those who
    support it. Often, both with good arguments. FWIW I agree with Randal
    and never use env myself.


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
     
    Michele Dondi, Oct 9, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    653
  2. Victor
    Replies:
    2
    Views:
    667
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    393
    ekzept
    Aug 10, 2007
  4. Replies:
    3
    Views:
    822
    Reedick, Andrew
    Jul 1, 2008
  5. John Gordon
    Replies:
    13
    Views:
    513
    Ian Kelly
    Dec 20, 2011
Loading...

Share This Page