[Python-ideas] Make undefined escape sequences have SyntaxWarnings

Mike Graham mikegraham at gmail.com
Wed Oct 10 22:45:06 CEST 2012


On Wed, Oct 10, 2012 at 4:16 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> On Wed, 10 Oct 2012 16:08:22 -0400
> Mike Graham <mikegraham at gmail.com> wrote:
>> On Wed, Oct 10, 2012 at 3:46 PM, Antoine Pitrou <solipsis-xNDA5Wrcr86sTnJN9+BGXg at public.gmane.org> wrote:
>> > On Wed, 10 Oct 2012 15:36:08 -0400
>> > Mike Graham <mikegraham at gmail.com> wrote:
>> >> The literal"\c" should be an error but in practice means "\\c". It's
>> >> probably too late to make this invalid syntax as it out to be, but I
>> >> wonder if a warning isn't in order, especially with the theoretical
>> >> potential of adding new string escapes in the future.
>> >
>> > -1. This will make life more difficult with regular expressions (and
>> > produce lots of spurious warnings in existing code).
>> >
>> > Regards
>> >
>> > Antoine.
>>
>> Regular expressions are difficult if you're remembering which escape
>> sequences exist and are easy if you're using raw string literals.
>
> That's a misconception, since as the re docs mention:
>
> “Most of the standard escapes supported by Python string literals are
> also accepted by the regular expression parser: [snip]”
>
> http://docs.python.org/dev/library/re.html
>
> In other words, whether you put "\t" or "\\t" in a regexp doesn't
> matter: it means the same to the regexp engine.
>
> Regards
>
> Antoine.

I'm not sure what misconception you're saying I have. An example of
when you have to remember what the escapes are is

>>> re.search("\by\b", "x y z") is None
True
>>> re.search("\\by\\b", "x y z") is None
False

Mike



More information about the Python-ideas mailing list