war*_*iuc 26 python regex unicode character-properties
我有一个字符串,我想从中提取3组:
'19 janvier 2012' -> '19', 'janvier', '2012'
Run Code Online (Sandbox Code Playgroud)
月份名称可能包含非ASCII字符,因此[A-Za-z]对我不起作用:
>>> 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'
>>>
Run Code Online (Sandbox Code Playgroud)
我可以使用,\w但它匹配数字和下划线:
>>> 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',)
>>>
Run Code Online (Sandbox Code Playgroud)
我尝试使用[:alpha:],但它不起作用:
>>> 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'
>>>
Run Code Online (Sandbox Code Playgroud)
如果我能以某种方式匹配\w没有[_0-9],但我不知道怎么办.即使我发现如何做到这一点,是否有一个现成的快捷方式[:alpha:],在Python中工作?
Tim*_*ker 52
您可以构造一个新的角色类:
[^\W\d_]
Run Code Online (Sandbox Code Playgroud)
而不是\w.翻译成英文,意思是"任何不是非字母数字字符的字符(与之[^\W]相同\w),但这也不是数字而不是下划线".
因此,它只允许使用Unicode字母(如果使用re.UNICODE编译选项).
| 归档时间: |
|
| 查看次数: |
9812 次 |
| 最近记录: |