首页 > 编程语言 > 详细

python 匹配中文和英文

时间:2015-07-21 20:31:47      阅读:192      评论:0      收藏:0      [点我收藏+]

在处理文本时经常会匹配中文名或者英文word,python中可以在utf-8编码下方便的进行处理。

中文unicode编码范围[\u4e00-\u9fa5]

英文字符编码范围[a-zA-Z]

此时匹配连续的中文或者英文就很方便了,例如:

>>> import re
>>> strings = u‘中国china美国American‘
>>> print strings
中国china美国American
>>> ch_pat = re.compile(ur‘[\u4e00-\u9fa5]+‘)
>>> en_pat = re.compile(‘[a-zA-Z]+‘)
>>> ch_words = ch_pat.findall(strings)
>>> en_words = en_pat.findall(strings)
>>> print ch_words
[u‘\u4e2d\u56fd‘, u‘\u7f8e\u56fd‘]
>>> print en_words
[u‘china‘, u‘American‘]

 

python 匹配中文和英文

原文:http://www.cnblogs.com/chybot/p/4665389.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!