带有unicode字符串的Python正则表达式
无法匹配python 2.7中的unicode字符串。 预期结果749130带有unicode字符串的Python正则表达式,python,regex,unicode,Python,Regex,Unicode,无法匹配python 2.7中的unicode字符串。 预期结果749130 >>> print match("\d+", u'\ufeff749130'.encode('utf-8')) None >>> print match("\d+", u'\ufeff749130') None >>> print match("\d+", u'\ufeff749130'.decode('utf-8')) Traceback (most recent
>>> print match("\d+", u'\ufeff749130'.encode('utf-8'))
None
>>> print match("\d+", u'\ufeff749130')
None
>>> print match("\d+", u'\ufeff749130'.decode('utf-8'))
Traceback (most recent call last):
....
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufeff' in position 0: ordinal not in range(128)
无需对unicode字符串使用
str.decode
。如注释中所述,您可能希望使用search
,因为match
仅匹配目标字符串的开头
>>> print search("\d+", u'\ufeff749130').group()
749130
match
从字符串开始匹配。使用搜索
。请为您的问题添加更多详细信息。你想对代码做什么?