在正则表达式中,例如当我在 Python 中使用时:
WORD = re.compile(r'\w+')
然后使用:
w = 'This is a test'
WORD.findall(w)
我得到:
['This', 'is', 'a', 'test']
现在我想将 half-space 字符,即 \u200c 视为普通的字母数字字符,所以如果我有:
w = 'This\u200cis a test'
然后,当我运行 WORD.findall(w) 时,我得到:
['This\u200cis', 'a', 'test']
我该怎么做?
请您参考如下方法:
使用character classes除了 \w (Python 3.x+) 之外,还包括 \u200c:
>>> import re
>>> re.findall(r'[\u200c\w]+', 'This\u200cis a test')
['This\u200cis', 'a', 'test']
在 Python 2.x 中,您需要使用 unicode:
>>> re.findall(u'[\u200c\w]+', u'This\u200cis a test')
[u'This\u200cis', u'a', u'test']
