develooper Front page | perl.perl5.porters | Postings from July 2013

Re: [perl #119095] Empty regular expression does not match in somecases

Thread Previous | Thread Next
From:
Paul Johnson
Date:
July 31, 2013 14:44
Subject:
Re: [perl #119095] Empty regular expression does not match in somecases
Message ID:
20130731144427.GC24578@pjcj.net
On Wed, Jul 31, 2013 at 07:34:07AM -0700, Petr Pisar wrote:
> Hello,
> 
> this code:
> 
> q{"} =~ m/"/;
> 
> if (q{a} =~ m//) {
>     print "TRUE\n";
> } else {
>     print "FALSE\n";
> }
> 
> should print TRUE, but it prints FALSE.
> 
> In other words, empty regular expression does not match. There is some side
> effect because it depends on previous regular match (the first line). If
> I change the first line anyhow, like m/"/ changing to m/./, the code starts
> working correctly.

I think this is one of those "it's a feature, not a bug" moments.
Though I'll admit that in over 20 years of using Perl, it's a feature
I've never made use of.

From perlop:

    The empty pattern //
            If the PATTERN evaluates to the empty string, the last
            successfully matched regular expression is used instead. In this
            case, only the "g" and "c" flags on the empty pattern are honored;
            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).

-- 
Paul Johnson - paul@pjcj.net
http://www.pjcj.net

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About