Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Strange regexp behavior

by Chmrr (Vicar)
on Mar 26, 2002 at 12:50 UTC ( [id://154381]=note: print w/replies, xml ) Need Help??


in reply to Strange regexp behavior

You're assuming that \1 is magical inside of character classes -- I don't think it is. For example, the following works, and does what you want it to do:

perl -pe 's/(")((?!\1).)*\1/check/g'

I don't remember how to tell Perl to spit out how it parsed the regex, but that might be useful in proving my point, so I think I'll do that now. ;>

Update: Ayup. Here's what perl says about your regex:

Compiling REx `(")[^\1]*\1' size 19 first at 3 1: OPEN1(3) 3: EXACT <">(5) 5: CLOSE1(7) 7: STAR(17) 8: ANYOF[\0\2-\377](0) 17: REF1(19) 19: END(0)

Notice that it's taking the \1 to be chr(1), not a backref to the 1st value captured. By contrast, using negative look-ahead:

Compiling REx `(")((?!\1).)*\1' size 24 first at 3 1: OPEN1(3) 3: EXACT <">(5) 5: CLOSE1(7) 7: CURLYM[2] {0,32767}(22) 11: UNLESSM[-0](17) 13: REF1(15) 15: SUCCEED(0) 16: TAIL(17) 17: REG_ANY(20) 20: SUCCEED(0) 21: NOTHING(22) 22: REF1(24) 24: END(0)

Well, it's not quite so clear what it's doing anymore (at least to me) But it works, eh?

perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Replies are listed 'Best First'.
Re: Re: Strange regexp behavior
by RMGir (Prior) on Mar 26, 2002 at 13:39 UTC
    chmrr++!

    I was trying to figure out the same thing, but couldn't remember how to decompile the regex.
    --
    Mike

      The easiest way is to use re 'debug'; You'll find that and much more under "Debugging regular expressions" in perldebguts.

      perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found