shawnhcorey wrote:
[...]
> Note that because of its level of precedence, the
>alteration applies to the whole pattern so $2 and $3 are undef when
>'lang' is matched.
[...]
Ok, so my problem was actually getting the precedence of the '|'-
operator wrong.
So /(lang)|(id)="(\S+)"/g translates to :
Mr. Shawn H. Corey wrote:
>
> Perl captures on a strict left-to-right policy. Start at the left of
> the regular expression and every opening parenthesis will be captured in
> the next numbered variable. For your first example:
>
> /(lang)|(id)="(\S+)"/g
>^ ^ ^
>$1 $2
op wrote:
Hello,
Hello,
I thought I had understood regular expression grouping relatively
well, untill I ran into the following behavior:
perl -e '
my $string = "";
while ($string =~ /(lang)|(id)="(\S+)"/g) {
print "\$1->|$1|, \$2->|$2|, \$3->|$3|\n";
}
'
outputs:
$1->||, $2->|id|, $3->|do
On Mon, 2008-08-04 at 03:28 -0700, op wrote:
> Hello,
> I thought I had understood regular expression grouping relatively
> well, untill I ran into the following behavior:
>
> perl -e '
> my $string = " lang=\"hindi\">";
> while ($string =~ /(lang)|(id)="(\S+)"/g) {
> print "\$1->|$1|, \$2->|$2|
Hello,
I thought I had understood regular expression grouping relatively
well, untill I ran into the following behavior:
perl -e '
my $string = "";
while ($string =~ /(lang)|(id)="(\S+)"/g) {
print "\$1->|$1|, \$2->|$2|, \$3->|$3|\n";
}
'
outputs:
$1->||, $2->|id|, $3->|dontcare|
$1->|lang|, $2-