代码之家  ›  专栏  ›  技术社区  ›  faiuwle

如何在python 2.7中使用re.unicode?

  •  2
  • faiuwle  · 技术社区  · 8 年前

    我试图使用re.unicode标志来匹配一个可能包含unicode字符的字符串,但它似乎不起作用。例如。:

    Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
    [GCC 5.4.0 20160609] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import re
    >>> r = re.compile(ur"(\w+)", re.UNICODE)
    >>> r.findall(u"test test test", re.UNICODE)
    []
    

    如果我不指定unicode标志,它就可以工作,但是很明显,它不会与unicode字符串一起工作。我需要做什么才能让这个工作正常进行?

    1 回复  |  直到 8 年前
        1
  •  6
  •   khelwood Muhammed Elsayed.radwan    8 年前

    第二个论点 r.findall 不是旗子,但 pos . 当已经在中指定标志时,不需要再次指定标志。 compile .

    >>> r = re.compile(ur"(\w+)", re.UNICODE)
    >>> r.findall(u'test test test')
    [u'test', u'test', u'test']