develooper Front page | perl.perl5.porters | Postings from October 2014

Re: Expected behavior of \Q inside a bracketed character class?

Thread Previous | Thread Next
From:
Eirik Berg Hanssen
Date:
October 16, 2014 15:57
Subject:
Re: Expected behavior of \Q inside a bracketed character class?
Message ID:
CAHAeAG5e-t+G_mv8eX49D=hCk7jTpg84euD7-5jtYxbf12eX_A@mail.gmail.com
On Thu, Oct 16, 2014 at 5:04 PM, Marco Moreno <mmoreno@pobox.com> wrote:
> On Wed, Oct 15, 2014 at 3:18 PM, Eric Brine <ikegami@adaelis.com> wrote:
>>
>>
>> \Q..\E are a feature of double quoted string literals and regex literals;
>> they have no thing to do with regular expressions. If you have one in your
>> regular expression, it's an error.
>
>
> Thanks for the explanation, Eric.  That helps clarify what's going on (i.e.
> \Q parsing takes place before regex parsing).
>
> However, can you elaborate what you mean by "if you have one (\Q..\E) in
> your regular expression, it's an error"?
>
> Isn't that the point of having \Q -- to provide quotemeta() in a regex?
> You're not suggesting that this example from the quotemeta() docs is
> improper?
>
> my $sentence = 'The quick brown fox jumped over the lazy dog';
> my $substring = 'quick.*?fox';
> $sentence =~ s{\Q$substring\E}{big bad wolf};
>
> I understand that as part of the parsing process, the \Q..\E no longer
> remains part of the compiled regex and should not be considered as such.  Is
> this what you meant by this?

  The \Q..\E is rewritten to \.\. in the parse phase the documentation
refers to as "interpolation".

  That is, as in: «If "'" is the delimiter, no interpolation is
performed on the PATTERN.»  Compare:

my $sentence = 'The quick brown fox jumped over the lazy dog';
my $substring = 'quick.*?fox';
$sentence =~ s'\Q$substring\E'big bad wolf';

  With this delimiter, the "interpolation" is not performed, and the
\Q and \E (as well as the $ and the substring) are once more revealed
not to be supported in regexen:

Unrecognized escape \Q passed through in regex; ...
Unrecognized escape \E passed through in regex; ...

  So yeah, they need to be "interpolated" first.


Eirik

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About