Letter class in re

Antoon Pardon antoon.pardon at rece.vub.ac.be
Mon Mar 9 08:26:51 EDT 2015


Op 09-03-15 om 12:17 schreef Tim Chase:
> On 2015-03-09 11:37, Wolfgang Maier wrote:
>> On 03/09/2015 11:23 AM, Antoon Pardon wrote:
>>> Does anyone know what regular expression to use for a sequence of
>>> letters? There is a class for alphanumerics but I can't find one
>>> for just letters, which I find odd.
>> how about [a-zA-Z] ?
> That breaks if you have Unicode letters.  While ugly, since "\w" is
> composed of "letters, numbers, and underscores", you can assert that
> the "\w" you find is not a number or underscore by using
>
>   (?:(?!_|\d)\w)

So if I understand correctly the following should be a regular expression for
a python3 identifier.

  (?:(?!_|\d)\w)\w+

It seems odd that one should need such an ugly expression for something that is
used rather frequently for parsing computer languages and the like.

-- 
Antoon Pardon 




More information about the Python-list mailing list