码迷,mamicode.com
首页 > 编程语言 > 详细

python 匹配中文和英文

时间:2015-07-21 20:35:03      阅读:127      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!