I looked through the documentation to find you a link on this (perlre, perlretut) but was unable to track one down. Essentially, the problem is that backreferences appear not to work as you seem to expect in a character class. I suspect this is because the backreference is not defined when the pattern is compiled. One work around for you would be to A bit of magic: executing Perl code in a regular expression:
if ( $line =~ /<(\w(?:[^ \/>]+))(?:(?:(?:\s+)([^=>]+)\s*=\s*("|')((??{ +"[^$3]*"}))\3))\s*\/>/) { print ("1=" . $1. "\n2=" . $2 . "\n3=" . $3 . "\n4=" . $4 . "\n5=" +. $5 . "\n"); }
In general, however, when you need to resort to using (?{...}) or (??{...}), you are trying too hard. Your code will likely be fragile and unmaintainable. See point 2 below.
Side notes:
Since double quotes interpolate (Quote and Quote like Operators), you could more simply write your print statement as:
print ("1=$1\n2=$2\n3=$3\n4=$4\n5=$5\n");
You probably shouldn't be parsing XML yourself as there are plenty of modules to do it for you (XML::Twig, XML::Simple, XML::Parser,...).
In reply to Re: PERL Regular expression
by kennethk
in thread PERL Regular expression
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |