python 正则表达式

学习网址:

http://www.runoob.com/regexp/regexp-syntax.html

 

import re

page_hero = '''<ul class="herolist clearfix"><li><a href="herodetail/194.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/194/194.jpg" width="91px" alt="苏烈">苏烈</a></li><li><a href="herodetail/195.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg" width="91px" alt="百里玄策">百里玄策</a></li><li><a href="herodetail/196.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/196/196.jpg" width="91px" alt="百里守约">百里守约</a></li><li><a href="herodetail/193.shtml" target="_blank"><img src="http://game.gtimg.cn/images/yxzj/img201606/heroimg/193/193.jpg" width="91px" alt="铠">铠</a></li></ul>
'''

href_pattern = r'\bhref=(.*?) .*?'
href_regex = re.compile(href_pattern, re.IGNORECASE)
for match in href_regex.finditer(page_hero):
    print("index=%s,href:%s\n"%(match.start(), match.group(1)))

name_pattern = r'.*?>(\w+?)<.*?'
name_regex = re.compile(name_pattern, re.IGNORECASE)    
for match in name_regex.finditer(page_hero):
    print("index=%s,name:%s\n"%(match.start(), match.group(1)))

jpg_pattern = r'\bsrc=(.*?) .*?'
jpg_regex = re.compile(jpg_pattern, re.IGNORECASE)    
for match in jpg_regex.finditer(page_hero):
    print("index=%s,name:%s\n"%(match.start(), match.group(1)))

? 匹配前面的子表达式零次或一次,或指明一个非贪婪限定符。要匹配 ? 字符,请使用 \?。

+ 匹配前面的子表达式一次或多次。要匹配 + 字符,请使用 \+。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值