带有unicode字符的Python正则表达式错误?

ak.*_*ak. 2 python regex unicode match character-properties

长话短说:

>>> re.compile(r"\w*").match(u"Français")
<_sre.SRE_Match object at 0x1004246b0>
>>> re.compile(r"^\w*$").match(u"Français")
>>> re.compile(r"^\w*$").match(u"Franais")
<_sre.SRE_Match object at 0x100424780>
>>> 
Run Code Online (Sandbox Code Playgroud)

为什么它不与Unicode字符匹配的字符串^,并$在正则表达式?据我所知,^代表字符串(行)的开头和$- 结尾.

ken*_*ytm 5

你需要指定UNICODE标志,否则\w只是等价[a-zA-Z0-9_],不包括字符' ç'.

>>> re.compile(r"^\w*$", re.U).match(u"Fran\xe7ais")
<_sre.SRE_Match object at 0x101474168>
Run Code Online (Sandbox Code Playgroud)