我正在尝试构建一个正则表达式来匹配负面unicode表情符号的不同可能组合。我遇到了与下面列表test_2中包含的表情符号类型相匹配的问题。虽然我相信符合表情符号的非字母数字符号被正确放置在正则表达式中,但是表情符号和左眼(名为eye1的捕获组)都不匹配......我怎么能解决它?感谢
neg_emoticon_regular = ur"""
[\((]? #optional left parenthesis
\s* #optional space
[\`\#\ ́]? #optional symbols between left parenthesis and left eye
(?P<eye1>[\ー\; \́\`\・\>Tt\ー\ ̄\−\-\゚~\_\.\>\*\/]) #left eye
\s* #optional space
[\。\。\Δ\-\人\O\0\.\Д\д\o\−\_\ω\ヘ\^\_]? #mouth
\s* #optional space
[(?P=eye1)\`\<\’] #right eye, usually will match left eye
[\A\#\;]? #optional symbols between right eye and right parenthesis
\s* #optional space
[\)\)]? #optional right parenthesis
"""
neg_emoticon_re = re.compile(neg_emoticon_regular, re.VERBOSE | re.UNICODE)
test_2 = ["(−_−#)","(-。-;","(-_-)"] #negative emoticons to match
for e in test_2:
e_uc_norm = unicodedata.normalize('NFKC', e.decode("utf-8"))
m = neg_emoticon_re.search(e_uc_norm)
if m: print "eye1:",m.group("eye1") #print the symbol that is supposed to be the left eye
print len(neg_emoticon_re.findall(e_uc_norm)), e_uc_norm
答案 0 :(得分:3)
在正则表达式中,[...]
是一组字符,因此[\((]
将匹配开括号或空格(可缩短为[( ]
)和{{1将匹配可选的空白字符或加号。