教程集 www.jiaochengji.com
教程集 >  Python编程  >  Python入门  >  正文 python3怎样匹配中文

python3怎样匹配中文

发布时间:2021-03-26   编辑:jiaochengji.com
教程集为您提供python3怎样匹配中文等资源,欢迎您收藏本站,我们将为您提供最新的python3怎样匹配中文资源

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

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

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

python学习网,大量的免费python视频教程,欢迎在线学习!

您可能感兴趣的文章:
python3怎样匹配中文
.net开发中常用正则表达式
PHP正则表达式匹配字符的方法汇总
Python3爬虫入门:正则表达式
jquery里的正则表达式说明
使用Perl常规表达式进行匹配
正则表达式(. ?)与(.*)区别
js正则regexp对象属性之lastParen属性
python3 re结合正则表达式如何使用?
python3 re闭包操作符是什么?有什么用?

[关闭]
~ ~