2014-11-23 67 views
1

半空间(\ u200c)在正则表达式例如在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'] 

我怎么能这样做?

回答

2

使用character classes包括\u200c除了\w(Python 3.x都有+):

>>> 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'] 
+0

感谢这正是我在寻找。 – TJ1 2014-11-23 00:58:27