Pregunta

Tengo una cadena de la que quiero extraer 3 grupos:

'19 janvier 2012' -> '19', 'janvier', '2012'

El nombre de mes podría contener caracteres no ASCII, así que [A-Za-z] no funciona para mí:

>>> import re
>>> re.search(ur'(\d{,2}) ([A-Za-z]+) (\d{4})', u'20 janvier 2012', re.UNICODE).groups()
(u'20', u'janvier', u'2012')
>>> re.search(ur'(\d{,2}) ([A-Za-z]+) (\d{4})', u'20 février 2012', re.UNICODE).groups()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'
>>> 

Podría usar \w Pero coincide con dígitos y subrayado:

>>> re.search(ur'(\w+)', u'février', re.UNICODE).groups()
(u'f\xe9vrier',)
>>> re.search(ur'(\w+)', u'fé_q23vrier', re.UNICODE).groups()
(u'f\xe9_q23vrier',)
>>> 

Traté de usar :alfa:, pero no funciona:

>>> re.search(ur'[:alpha:]+', u'février', re.UNICODE).groups()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'NoneType' object has no attribute 'groups'
>>> 

Si de alguna manera pudiera coincidir \w sin que [_0-9], pero no sé cómo. E incluso si descubro cómo hacer esto, ¿hay un atajo listo como [:alpha:] ¿Qué funciona en Python?

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top