[Python-ideas] Verbatim names (allowing keywords as names)

Tim Peters tim.peters at gmail.com
Tue May 15 23:45:47 EDT 2018

[Terry Reedy]
> ...
> I believe avoiding tagging raw names as keywords could be done by adjusting
> the re for keywords

Yup - it should just require adding a negative lookbehind assertion; e.g.,

>>> import re
>>> keypat = r"(?<!\\)\b(if|while|for)\b"
>>> re.search(keypat, r"yup! while")
<_sre.SRE_Match object; span=(5, 10), match='while'>
>>> re.search(keypat, r"nope! \while") # None

The "(?<!\\)" part means "if what follows me matched, pretend it
didn't match if the character before it is a backslash - provided
there _is_ a character before it".

More information about the Python-ideas mailing list