Home  >  Article  >  Backend Development  >  python splits mixed Chinese and English strings

python splits mixed Chinese and English strings

高洛峰
高洛峰Original
2016-10-19 13:48:263810browse


#coding=utf-8
import re
s = 'hi新手oh'.decode('utf-8') #举个栗子是字符串s,为了匹配下文的unicode形式,所以需要解码
p = re.compile(ur'[\u4e00-\u9fa5]') #这里是精髓,[\u4e00-\u9fa5]是匹配所有中文的正则,因为是unicode形式,所以也要转为ur
   
print p.split(s) #使用re库的split切割

   


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn