Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: regex corrupting separate regex

by blokhead (Monsignor)
on Feb 12, 2010 at 22:20 UTC ( #822942=note: print w/ replies, xml ) Need Help??


in reply to regex corrupting separate regex

From perlop (under "Quote-like operators"):

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 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).

This is an odd "feature" of Perl, and it's very easy to get bitten by it in m/$pattern/. It's happened to me several times. Perhaps a good solution is m/(?:$pattern)/ or m/(?:)$pattern/ or something similar, so that the pattern is never empty.

Update:

$ perl -le 'print 1 if "foo" =~ /o/; print 2 if "asdf" =~ //' 1 $ perl -le 'print 1 if "foo" =~ /z/; print 2 if "asdf" =~ //' 2 $ perl -le 'print 1 if "foo" =~ /o/; print 2 if "asdf" =~ /(?:)/' 1 2

blokhead


Comment on Re: regex corrupting separate regex
Select or Download Code
Re^2: regex corrupting separate regex
by boxofrox (Initiate) on Feb 13, 2010 at 01:22 UTC
    Thank you, blokhead. That's what I trying to find in perlre.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://822942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-10-26 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (153 votes), past polls