python正则表达学习

python替换 主要是利用 python 的正则表达,python的正则表达功能比较强大。我就介绍下我经常用的几个方法。可能说的详细,如果要深入研究的话。还是

要去看下python的文档了.

废话不多说。马上开始介绍

一般我会用re.compile 来建立一个正则对象。python 文档上介绍好象这个会加快匹配的速度

link = re.compile(r'\d+')

然后生成的话。我就可以开始匹配了

content = link.search (content).groups()

或者

content = link.fetchAll (content)

返回一个列表

还有sub等方法

正则这块的话我也学的不好。所以有时匹配一些字符串的时候会吃力。冒大汗就当减肥,,,

下面上一段我写的代码:

import glob
import re
import string
image = re.compile(r'http://images.wauee.com/ring/|/static/')
dir = glob.glob (r"/home/workspace/ring/tag/*.html")
for i in dir:
xxx = open(i, 'r')
content =  xxx.readlines()
strcontent = ''
filecontent = []
for k in content:
new = image.sub('../static/', k)
filecontent.append(new)
strcontent = "".join(filecontent)

aa = open(i, 'w')
aa.write(strcontent)
print 'success'

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值