Front page | perl.perl5.porters |
Postings from May 2012
Re: "\" does not escape meta chars if also delim
From:
Klaus
Date:
May 29, 2012 13:23
Subject:
Re: "\" does not escape meta chars if also delim
Message ID:
c9d0010f-c353-4c7e-bd57-0870ecb6dfb0@cu1g2000vbb.googlegroups.com
On 29 mai, 08:39, perlbug-follo...@perl.org ("Eric Brine") wrote:
>
> About "\", perlre says:
>
> "Quote the next metacharacter."
>
> "So anything that looks like \\, \(, \), \<, \>, \{, or \} is always
> interpreted as a literal character, not a metacharacter."
>
> "Any single character matches itself, unless it is a metacharacter with a
> special meaning described here or above. You can cause characters that
> normally function as metacharacters to be interpreted literally by
> prefixing them with a "\" (e.g., "\." matches a ".", not any character;
> "\\" matches a "\"). This escape mechanism is also required for the
> character used as the pattern delimiter."
>
> Yet when "\" is used to escape a char that's both delimiter and meta, the
> escaped character doesn't cease being meta as documented.
>
> >perl -E" say 'a' =~ m/\./ ? 'XXX' : 'ok' "
>
> ok
>
> >perl -E" say 'a' =~ m.\.. ? 'XXX' : 'ok' "
>
> XXX
I would consider this a bug.
As far as I can tell, this comes from the over-simplified / wrong
quoting/parsing of regular expressions.
I might be wrong, but here is an example of how I think regular
expressions get processed, that involves { } as delimiters of that
regular expression:
imagine a regexp that matches 2 a's, followed by a literal \{, a
literal 2 and a literal \}:
m{a{2}a\{2\}}
This regexp gets pre-processed (and here is where, in my opinion, the
damage occurs) to replace any non-escaped { or } into an escaped \{ \}
This results in the following:
m{a\{2\}a\{2\}}
Now the delimiters m{...} get removed
a\{2\}a\{2\}
and finally all escaped delimiters are "unescaped"
a{2}a{2}
which results in a regexp /a{2}a{2}/ that matches 4 consecutive 'a's
That's how I think regular expressions are handled, but I think this
is a bug.
-
Re: "\" does not escape meta chars if also delim
by Klaus