当前位置:Gxlcms > Python > python正则表达式中的中文匹配例子

python正则表达式中的中文匹配例子

时间:2021-07-01 10:21:17 帮助过:97人阅读

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

人气教程排行