简体   繁体   中英

Unicode classes in regex with Python2

Is it possible?

This code works in Python3 :

In [1]: import re

In [2]: re.split(r'\W+', 'Les Misérables')
Out[2]: ['Les', 'Misérables']

But it does not work in Python2 :

In [1]: import re

In [2]: re.split(r'\W+', u'Les Misérables')
Out[2]: [u'Les', u'Mis', u'rables']

This does not work either (tested on Linux with es_ES.UTF-8 locale):

In [1]: import locale

In [2]: locale.setlocale(locale.LC_ALL, 'es_ES.UTF-8')
Out[2]: 'es_ES.UTF-8'

In [3]: import re

In [4]: re.split(ur'\W+', u'Les Misérables', re.U|re.L)
Out[4]: [u'Les', u'Mis', u'rables']

Is there any way to get regex working with Unicode in Python2 ?

Note: The question is about getting Unicode-aware matches. I know I can rewrite the above regex to separate words using only ASCII classes.

Your mistake is that you're adding flags on the wrong place (flags should be the 4th param).

>>> import re
>>> re.split(r'(?u)\W+', u'Les Misérables')
[u'Les', u'Mis\xe9rables']
>>> re.split(ur'\W+', u'Les Misérables', 0, re.U)
[u'Les', u'Mis\xe9rables']

To avoid these issues I'd recommend using inline flags (like (?u) above).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM