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 :
  either match the string "lang" and nothing else
  _or_
  match the other, longer part of the regexp

Thanks, and sorry I made you write quite a bit more than was actually
necessary to help me.



On 4 elo, 13:28, [EMAIL PROTECTED] (Op) wrote:
> Hello,
> I thought I had understood regular expression grouping relatively
> well, untill I ran into the following behavior:
>
> perl -e '
> my $string = "<div name=\"abcd\" id=\"dontcare\" class=\"testvalue\"
> lang=\"hindi\">";
> while ($string =~ /(lang)|(id)="(\S+)"/g) {
>   print "\$1->|$1|, \$2->|$2|, \$3->|$3|\n";}
>
> '
> outputs:
> $1->||, $2->|id|, $3->|dontcare|
> $1->|lang|, $2->||, $3->||
>
> Soon after I realised my mistake and replaced
> (lang)|(id) with (lang|id),
> getting the ouput I expected:
> $1->|id|, $2->|dontcare|, $3->||
> $1->|lang|, $2->|hindi|, $3->||
>
> However, I still would have expected the first version to capture the
> same strings, even with the superfluous parentheses. So, I obviously
> haven't understood as much of regexp capturing as I had hoped, maybe
> someone could enlighten me on this? I did browse through the parts on
> capturing/grouping in perlre, perlreref and perlretut, but didn't find
> anything that would have made me understand this.
>
> Thanks in advance,
> -op


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to