[Python-Dev] one last SRE headache

Andrew Kuchling akuchlin@mems-exchange.org
2000年8月31日 15:46:03 -0400


On Thu, Aug 31, 2000 at 09:46:54PM +0200, Fredrik Lundh wrote:
>can anyone tell me how Perl treats this pattern?
> r'((((((((((a))))))))))41円'

>if I understand this correctly, Perl treats as an *octal* escape
>(chr(041) == "!").

Correct. From perlre:
 You may have as many parentheses as you wish. If you have more
 than 9 substrings, the variables 10,ドル 11,ドル ... refer to the
 corresponding substring. Within the pattern, 10,円 11,円
 etc. refer back to substrings if there have been at least that
 many left parentheses before the backreference. Otherwise (for
 backward compatibility) 10円 is the same as 010,円 a backspace,
 and 11円 the same as 011,円 a tab. And so on. (1円 through 9円
 are always backreferences.) 
In other words, if there were 41 groups, 41円 would be a backref to
group 41; if there aren't, it's an octal escape. This magical
behaviour was deemed not Pythonic, so pre uses a different rule: it's
always a character inside a character class ([41円] isn't a syntax
error), and outside a character class it's a character if there are
exactly 3 octal digits; otherwise it's a backref. So 41円 is a backref
to group 41, but 041円 is the literal character ASCII 33.
--amk

AltStyle によって変換されたページ (->オリジナル) /