in reply to negative lookbehind and VERY strange capture

Expanding out your regex it becomes obvious

use strict; use warnings; my $s = '"toto"'; if ($s =~ m[^ (?<!\\)" ( ( (?<=\\)"|[^"] )+ # $2 ) # $1 (?<!\\)" $]x ) { print "It matches!\n"; print $1 . "\n"; print $2 . "\n"; }

You have two nested pairs of capturing parens, the inner pair with a repeat. So the outer pair captures everything matched by the inner repetition, and the inner pair captures the last thing it matches. Hence "toto" and the last character of that "o".


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: negative lookbehind and VERY strange capture
by Denis.Beurive (Initiate) on Sep 18, 2016 at 11:53 UTC

    Hello BrowserUK,

    Thank you very much for your answer. I just learn something. I put « non-capturing parentheses » and it did the trick :

    my $s = '"abcd"'; print "Try for \"$s\":\n"; if ($s =~ /^(?<!\\)"((?:(?<=\\)"|[^"])*)(?<!\\)"$/) { print "It matches!\n"; print '$1: ' . $1 . "\n"; print '$2: ' . (defined($2) ? "\$2 is defined\n" : "\$2 is NOT defin +ed\n"); } else { print "It does not match!\n"; }

    However, as Corion pointed out, my regular expression does not work for this string « abcd\\ ». I’ll try some new approaches.

    Best regards

    Denis