nwboy74 has asked for the wisdom of the Perl Monks concerning the following question:

Why does this match?

my $s = '<a href=\'2010-coachmen-mirada-bunk-house-w2-slides-for-sale- +TX-i111851\' class=\'uviss-rv_inventoryListMoreInfoLink\'>'; if ($s =~ /href=(['"])([^\1>]+?for-sale[^\1>]+?)\1>/i) { print "$2\n"; }

Doesn't [^\1>]+? mean look for a sequence of characters not containing \1 or >? I get this result:

2010-coachmen-mirada-bunk-house-w2-slides-for-sale-TX-i111851' class='uviss-rv_inventoryListMoreInfoLink

Replies are listed 'Best First'.
Re: Pattern Matching
by AnomalousMonk (Archbishop) on Jan 14, 2010 at 21:32 UTC

    In a character set,  "\1" is the character whose ASCII value is octal 01. It matches the same character (in a character set) as  "\x01" does. Outside a character set, it would be a backreference to the first capture group.

    >perl -wMstrict -le "print '1. no match octal 01' if qq{\1} =~ m{ [^\1] }xms; print '2. match numeral 1' if qq{1} =~ m{ [^\1] }xms; print '3. match octal 01' if qq{\1} =~ m{ [\1] }xms; print '4. no match numeral 1' if qq{1} =~ m{ [\1] }xms; " 2. match numeral 1 3. match octal 01
Re: Pattern Matching
by ahmad (Hermit) on Jan 14, 2010 at 21:22 UTC

    No it doesn't, \1 is a backreference to the first match.

    [^\1>] means match anything that do not match $1 (the first match) or >

    Update: See the answers below, I missed the character class thing.

      Backreferences don't work in character classes. Try this:

      $ perl -e 'if ("aa\\"=~/(.)[^\1]/) { print "$&\n"}'

      If backreferences work in character classes, we should see the output as a\.