6

Suppose I want to match a lowercase letter followed by an uppercase letter, I could do something like

re.compile(r"[a-z][A-Z]")

Now I want to do the same thing for unicode strings, i.e. match something like 'aÅ' or 'yÜ'.

Tried

re.compile(r"[a-z][A-Z]", re.UNICODE)

but that does not work.

Any clues?

tchrist
  • 78,834
  • 30
  • 123
  • 180
repoman
  • 3,485
  • 2
  • 16
  • 15

1 Answers1

7

This is hard to do with Python regex because the current implementation doesn't support Unicode property shortcuts like \p{Lu} and \p{Ll}.

[A-Za-z] will of course only match ASCII letters, regardless of whether the Unicode option is set or not.

So until the re module is updated (or you install the regex package currently in development), you either need to do it programmatically (iterate through the string and do char.islower()/char.isupper() on the characters), or specify all the unicode code points manually which probably isn't worth the effort...

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 1
    This was useful. I only have to deal with Danish letters. So adding 'æøå' and 'ÆØÅ' is probably OK. – repoman Sep 13 '11 at 07:07