Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unicode classes in regex with Python2

Tags:

python

regex

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.

like image 673
Alicia Avatar asked Mar 16 '26 01:03

Alicia


1 Answers

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).

like image 109
georg Avatar answered Mar 18 '26 03:03

georg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!