该篇记录正则表达式的一些内容,后续会一直补充
利用re提取链接内容:
# -*- coding:utf-8 -*-
import re
#方法一
#ret = re.search(r"www.baidu.com","<p>www.baidu.com</p>")
#方法二
#ret = re.search(r".*\Bai\B.*","<p>www.baidu.com</p>")
#方法三
#ret = re.search(r"<[a-z]>.*</[a-z]>","<p>www.baidu.com</p>")
#方法四
#ret = re.search(r"<([a-z]*)>.*</\1>","<p>www.baidu.com</p>")
#方法五
#ret = re.match(r"<(?P<name1>\w*)>.*</(?P=name1)>","<p>www.baidu.com</p>")
print ret.group()
2:处理一些网址:
http://www.interoem.com/messageinfo.asp?id=35
http://3995503.com/class/class09/news_show.asp?id=14
http://lib.wzmc.edu.cn/news/onews.asp?id=769
http://www.zy-ls.com/alfx.asp?newsid=377&id=6
http://www.fincm.com/newslist.asp?id=415
deal with :
deal with......
http://www.interoem.com/
http://3995503.com/
http://lib.wzmc.edu.cn/
http://www.zy-ls.com/
http://www.fincm.com/
源码如下:
# -*- coding:utf-8 -*-
import re
a1="http://www.interoem.com/messageinfo.asp?id=35"
a2="http://3995503.com/class/class09/news_show.asp?id=14"
a3="http://lib.wzmc.edu.cn/news/onews.asp?id=769"
a4="http://www.zy-ls.com/alfx.asp?newsid=377&id=6"
a5="http://www.fincm.com/newslist.asp?id=415"
l1 = []
l1.append(a1)
l1.append(a2)
l1.append(a3)
l1.append(a4)
l1.append(a5)
for i in range(5):
print l1[i]
print "*"*50
print " deal with......"
for i in range(5):
ret = re.sub(r"(http://.+?/).*",lambda x:x.group(1),l1[i])
print ret