28

I would like to match all lowercase letter forms in the Latin block. The trivial '[a-z]' only matches characters between U+0061 and U+007A, and not all the other lowercase forms.

I would like to match all lowercase letters, most importantly, all the accented lowercase letters in the Latin block used in EFIGS languages.

[a-zà-ý] is a start, but there are still tons of other lowercase characters (see http://www.unicode.org/charts/PDF/U0000.pdf). Is there a recommended way of doing this?

FYI I'm using Python, but I suspect that this problem is cross-language.

Python's builtin "islower()" method seems to do the right checking:

lower = ''
for c in xrange(0,2**16): 
  if unichr(c).islower(): 
    lower += unichr(c)

print lower 
1
  • 1
    Regex implementations vary nontrivially; there's no "lowercase letter" character class in Python's regexes. Commented Mar 7, 2011 at 20:59

4 Answers 4

14

Python does not currently support Unicode properties in regular expressions. See this answer for a link to the Ponyguruma library which does support them.

Using such a library, you could use \p{Ll} to match any lowercase letter in a Unicode string.

Every character in the Unicode standard is in exactly one category. \p{Ll} is the category of lowercase letters, while \p{L} comprises all the characters in one of the "Letter" categories (Letter, uppercase; Letter, lowercase; Letter, titlecase; Letter, modifier; and Letter, other). For more information see the Character Properties chapter of the Unicode Standard. Or see this page for a good explanation on use of Unicode in regular expressions.

4
  • 9
    FYI. The 'regex' module (pypi.python.org/pypi/regex) says it supports Unicode Character properties, but it doesn't seem to work properly for '\p{Ll}', but does work for some other properties. YMMV.
    – slacy
    Commented Mar 7, 2011 at 21:34
  • Thanks, @slacy - I didn't know that about the pypi regex module.
    – Avi
    Commented Mar 7, 2011 at 21:55
  • Good. It also works with .NET regex libabry (tested with Cyrillic). Use \p{Lu} to match upper case characters. Commented Aug 2, 2012 at 2:00
  • 1
    As of today, the regex module worked for me with \p{Ll}.
    – kepler
    Commented Apr 19, 2018 at 9:55
8

Looks as though this recipe posted back in the old 2005

import sys, re

uppers = [u'['] 
for i in xrange(sys.maxunicode): 
  c = unichr(i) 
  if c.isupper(): uppers.append(c) 
uppers.append(u']') 
uppers = u"".join(uppers) 
uppers_re = re.compile(uppers) 

print uppers_re.match('A')

is still relevant.

5

You might want to have a look at regular-expressions.info.

However, as far as I know there's no character class or modifier that expresses "lower case characters only" (and not every language has lower case characters), so I'd say you might have to use multiple ranges (possible almost as many as there are unicode blocks.

Edit: reading a bit more on this, there might be a way: [\p{Ll}\p{Lo}] which means lowercase characters with an upper case variant or characters that don't have lower case and upper case (in case of chinese characters for example).

Regex [\p{Ll}\p{Lo}]+ matches test string àÀhelloHello你好Прывітанне and replacing the matches with x results in xÀxHxПx whereas replacing the matches of [\p{Ll}]+ results in xÀxHx你好Пx (note the Chinese characters that were not matched).

1
  • 4
    This is interesting, but Python regular expressions don't include \p :(
    – slacy
    Commented Mar 7, 2011 at 21:07
0

if you use \p{L} it will match any unicode letter. check the examples here. You can also combine it with \p{M} to match Hebrew-esqe languages that include diacritic marks. (\p{L}|\p{M})+

EDIT:

I missed the part about only lowercase letters the first time around. \p{L} will match all letters, \p{Ll} will match lowercase only.

1
  • 5
    There's no \p in Python's regex. Commented Mar 7, 2011 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.