Python正则使用示例

原文地址
使用python从一段文本中使用正则匹配自己需要的文本,如果使用search,只会进行一次匹配,group(0)里面的内容是全匹配,group(1)里面的内容是括号里面的子正则。如果需要全部匹配就使用findall,我这里的就是全匹配,返回的是一个列表,每一项内容就是括号里的字正则匹配内容。


#!/usr/bin/env python

import re
import sys
import json

class RegexImg(object):
    def __init__(self):
        self.pattern = re.compile(r'{\"img_src\":\"(.*?)",\"rcv_url\"')

    def process(self, str):
        return self.pattern.findall(str)


def main():
    dict = sys.argv[1]
    input = sys.argv[2]
    output = sys.argv[3]

    regex_img = RegexImg()
    dt = {}

    with open(dict, 'r') as fd, open(input, 'r') as fin, open(output, 'w') as fout:
        for line in fd:
            parts = line.rstrip().split('\t')
            if len(parts) != 2:
                print "wrong format line:%s" % line
                continue
            dt[parts[0]] = parts[1]
        for line in fin:
            parts = line.rstrip().split('\t')
            if len(parts) != 3:
                print "wrong format line:%s" % line
                continue
            ideaid = parts[0]
            material = parts[2]
            matchs = regex_img.process(material)
            if len(matchs) != 3:
                print "wrong img num:%d" % len(matchs)
                print matchs
                continue
            if ideaid not in dt:
                print "ideaid:%s has not its planid" % (ideaid)
                continue
            tmp_dt = {}
            tmp_dt["planid"] = dt[ideaid]
            tmp_dt["img_src"] = matchs
            json_str = json.dumps(tmp_dt)
            fout.write("%s\n" % json_str)

if __name__ == "__main__":
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值