Regex \w
似乎忽略了我的 Unicode 字符串。
我创建了以下函数:
extras.py
# -*- coding: utf-8 -*-
def test(word):
print re.sub(r'[^\w]+', '', word, re.U)
以及来自 django shell:
import extras
extras.test(u'שלום')
输出是一个空字符串,但在本例中它应该与输入相同。
正则表达式的目的是仅保留字母数字字符,但它不起作用。但它可以与 ASCII 一起使用。
可能是什么问题?
最佳答案
使用原始 unicode 字符串作为模式,并确保使用 flags
参数:
# -*- coding: utf-8 -*-
def test(word):
print re.sub(ur'[^\w]+', u'', word, flags=re.U)
然后:
In [9]: test(u'Ã')
Ã
关于python - Django 正则表达式 unicode 忽略\w 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11747625/