python正则表达式中的中文匹配例子
来源:懂视网
责编:小采
时间:2020-11-27 14:27:29
python正则表达式中的中文匹配例子
python正则表达式中的中文匹配例子:#coding=utf-8 import re from urllib2 import urlopen webpage = urlopen('http://www.baidu.com') #获取百度页面的信息 text = webpage.read() #读取为文本 tmp = text.decode('utf8')
导读python正则表达式中的中文匹配例子:#coding=utf-8 import re from urllib2 import urlopen webpage = urlopen('http://www.baidu.com') #获取百度页面的信息 text = webpage.read() #读取为文本 tmp = text.decode('utf8')

#coding=utf-8
import re
from urllib2 import urlopen
webpage = urlopen('http://www.baidu.com') #获取百度页面的信息
text = webpage.read() #读取为文本
tmp = text.decode('utf8') #对原文本进行utf8转码, 此处要跟代码的编码格式一致
pat = '<title>(.*)?([u4e00-u9fa5]*)?</title>' #对中文进行匹配
re.escape(pat) #对匹配模式中需要转义的符号进行转义
pat = re.compile(pat) #compile一下
m = re.search(pat,tmp)
title = m.group(1)
print title
webpage.close()
python正则表达式中的中文匹配例子
python正则表达式中的中文匹配例子:#coding=utf-8 import re from urllib2 import urlopen webpage = urlopen('http://www.baidu.com') #获取百度页面的信息 text = webpage.read() #读取为文本 tmp = text.decode('utf8')