Skip to content

Instantly share code, notes, and snippets.

@acdha acdha/gist:5111687
Created Mar 7, 2013

What would you like to do?
Fun with regular expressions in Python and Unicode
I noticed an interesting failure while using re.match / re.sub to look for non-Cyrillic characters in allegedly Russian text:
>>> re.sub(r'[\s\u0400-\u0527]+', ' ', 'Архангельская губерния', flags=re.IGNORECASE)
'Архангельская губерния'
>>> re.sub(r'[\s\u0400-\u0527]+', '', 'Архангельская губерния', flags=0)
The same is true in Python 2.7, although you need to use ur'' patterns for the literals to be expanded:
>>> re.sub(ur'[\s\u0400-\u0527]+', '', u'Архангельская губерния', flags=re.IGNORECASE|regex.UNICODE)
In contrast, the regex module behaves as expected:
>>> regex.sub(ur'[\s\u0400-\u0527]+', '', u'Архангельская губерния', flags=regex.IGNORECASE|regex.UNICODE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.