python 正则表达式----练习题目

该篇记录正则表达式的一些内容,后续会一直补充

利用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

 

 

 

 

 


 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值