Python re正则匹配中文

Python re正则匹配中文,其实非常简单,把中文的unicode字符串转换成utf-8格式就可以了,然后可以在re中随意调用

unicode中中文的编码为/u4e00-/u9fa5,因此正则表达式u”[\u4e00-\u9fa5]+”可以表示一个或者多个中文字符

>>> import re>>> s='中文:123456aa哈哈哈bbcc'.decode('utf8')>>> su'\u4e2d\u6587\uff1a123456aa\u54c8\u54c8\u54c8bbcc'>>> print s中文:123456aa哈哈哈bbcc>>> re.match(u"[\u4e00-\u9fa5]+",s)>>> pat='中文'.decode("utf8")>>> re.search(pat,s)>>> newpat='这里是中文内容'.decode("utf8")>>> news=re.sub(pat,newpat,s)>>> print news这里是中文内容:123456aa哈哈哈bbcc

参考资料:

http://blog.csdn.net/freedomqx/article/details/6237654

https://www.evernote.com/shard/s229/sh/0490e5c6-020f-4edc-9b35-1a4dcf1bef2d/192c189e398edf9a8c8f7abce1ac940a

Python re正则匹配中文

相关文章:

你感兴趣的文章:

标签云: