zoukankan      html  css  js  c++  java
  • 正则习题

    1、用正则过滤出json串里的两个字段:

    import requests
    import re


    url = "http://qwd.jd.com/fcgi-bin/qwd_searchitem_ex?skuid=26878432382%7C1658610413%7C26222795271%7C25168000024%7C11731514723%7C26348513019
    %7C20000220615%7C4813030%7C25965247088%7C5327182%7C19588651151%7C1780924%7C15495544751%7C10114188069%7C27036535156%7C10123099847%7C26016197600
    %7C10503200866%7C16675691362%7C15904713681"

    session = requests.session()
    r = session.get(url)
    html = r.text

    reg = re.compile(r"skuid.*?(d+).*?skuimgurl":"(S+)"")
    result = reg.findall(html)
    print(result)

    2、过滤文件内的每一个upstream和location配置并分别创建文件夹存放:

    import codecs
    import re
    import os

    regUpstream = re.compile(r"s*(upstreams+(S+)s+{[^}]+})")

    with codecs.open("ga10.wms5.jd.com.txt") as fu:
    textList = regUpstream.findall(fu.read())
    if not os.path.exists("upstream"):
    os.mkdir("upstream")
    os.chdir("upstream")
    for item in textList:
    with codecs.open(item[1], "w") as fw:
    fw.write(item[0])
    os.chdir("..")

    regLocation = re.compile(r"(locations+/(S+)/s+{s+proxy_next_upstream.*[^]]*?})")
    with codecs.open("ga10.wms5.jd.com.txt") as fl:
    textLocation = regLocation.findall(fl.read())
    if not os.path.exists("location"):
    os.mkdir("location")
    os.chdir("location")
    for each in textLocation:
    file = each[1] + ".locaion.conf"
    with codecs.open(file, "w") as flw:
    flw.write(each[0])
  • 相关阅读:
    [Noi2011]阿狸的打字机
    Bzoj3530: [Sdoi2014]数数
    Bzoj2037: [Sdoi2008]Sue的小球
    Bzoj4869: [Shoi2017]相逢是问候
    Bzoj1899: [Zjoi2004]Lunch 午餐
    Bzoj3884: 上帝与集合的正确用法
    UVA10692:Huge Mods
    Bzoj1009: [HNOI2008]GT考试
    Bzoj1212: [HNOI2004]L语言
    【国家集训队2012】tree(伍一鸣)
  • 原文地址:https://www.cnblogs.com/Jweiqing/p/8989672.html
Copyright © 2011-2022 走看看