python 正则表达式匹配中文
文件编码为 utf-8
设置默认编码为 utf-8
中文需要转换为 \u 形式的编码,也就是 编码,
轮换方法,cmd 下执行 python 进入 python 命令提示符模式
执行:
>>> u'中文'.encode('unicode_escape')
输出为:
'\\u4e2d\\u6587'
使用示例:
import sys defaultencoding = 'utf-8' if sys.getdefaultencoding() != defaultencoding: reload(sys) sys.setdefaultencoding(defaultencoding) import itchat,time,re from itchat.content import * @itchat.msg_register([TEXT]) def text_reply(msg): print(msg['Text']) match = re.search(u'[\u5e74]',msg['Text']) print(match) if match: itchat.send(('那我就祝您狗年大吉大利'), msg['FromUserName']) @itchat.msg_register([PICTURE, RECORDING, VIDEO, SHARING]) def other_reply(msg): itchat.send(('那我就祝您狗年大吉大利'), msg['FromUserName']) itchat.auto_login(enableCmdQR=True,hotReload=True) itchat.run()