Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87195 > unrolled thread

Re: Letter class in re

Started byWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
First post2015-03-09 14:32 +0100
Last post2015-03-09 14:32 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Letter class in re Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2015-03-09 14:32 +0100

#87195 — Re: Letter class in re

FromWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Date2015-03-09 14:32 +0100
SubjectRe: Letter class in re
Message-ID<mailman.205.1425908030.21433.python-list@python.org>
On 03/09/2015 01:26 PM, Antoon Pardon wrote:
> 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+
>

No, that is not it. For one thing, a leading underscore is fine in 
identifier names. That is easy to fix in your expression though.
Another thing are the Other_ID_Start and Other_ID_Continue categories 
defined in http://www.unicode.org/Public/6.3.0/ucd/PropList.txt, e.g.,

 >>> '\u212E'
'℮'
 >>> ℮ = 10
 >>> ℮
10

though ℮ is not included in \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.
>

There is str.isidentifier, which returns True if something is a valid 
identifier name:

 >>> '℮'.isidentifier()
True


[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web