Letter class in re

Tim Chase python.list at tim.thechases.com
Mon Mar 9 07:17:36 EDT 2015


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)

as demonstrated:

Python 3.2.3 (default, Feb 20 2013, 14:44:27) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "áèîöçñ"
>>> import re
>>> r = re.compile(r'^[a-zA-Z]*$', re.U)
>>> r.match(s)
>>> r = re.compile(r"^(?:(?!_|\d)\w)*$", re.U)
>>> r.match(s)
<_sre.SRE_Match object at 0x7fb205da9850>

I do miss that Python used "\a" for "start of string" rather than
"alphabetic" like Vim does (and correspondingly "\A" for "not an
alphabetic").

-tkc



More information about the Python-list mailing list