首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python3–正则匹配

python3–正则匹配

作者头像
老七Linux
发布2018-05-09 17:17:52
4.8K0
发布2018-05-09 17:17:52
举报

正则匹配练习一:

给定一段字符串,利用 https://regex101.com/ 此网站,筛选出需要的数据:

skuid的value,和skuimgurl的value。

r"\"skuid\":\"(\d+)\",\s+\S+\s\S+,\s\"skuimgurl\":\"(\S+)\","

需要什么value 就把什么value使用括号 括起来 即可!

抓取内容(类似于后期将要学到的爬虫)

import re
import requests

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()
result = session.get(url)
tt = result.text
print(tt)

对内容按照刚刚编写的正则进行筛选:

import re
import requests

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()        ##定义一个session
result = session.get(url)           ##获取一个url
tt = result.text                    ##把获取到的内容输入到文本
# print(tt)

reg = re.compile(r"\"skuid\":\"(\d+)\",\s+\"\S+\s+\"skuurl\"\S+\s+\"skuimgurl\":\"(\S+)\",")             ##定义正则规则,括号内的 r" 代表着脱意
aa = reg.findall(tt)        ##匹配正则规则
print(aa)
mark
mark

输出:

输出结果为多个tuple组成的list:

[('26878432382', 'https://img13.360buyimg.com/n7/jfs/t18226/169/1318243724/390477/5b0718ff/5ac44edcNa350dbd9.jpg'), ('5327182', 'https://img13.360buyimg.com/n7/jfs/t17461/138/1837663326/68820/5f8da5cd/5ad9b1e2N42bce837.jpg'), ('11731514723', 'https://img13.360buyimg.com/n7/jfs/t19231/337/2147939016/196162/4210a6ae/5aea6250N0235cd05.jpg'), ('19588651151', 'https://img13.360buyimg.com/n7/jfs/t11341/60/1553062810/120774/ab9534ff/5a02c3f4Naebe34b7.jpg'), ('15495544751', 'https://img13.360buyimg.com/n7/jfs/t18088/43/2048465630/167669/dd3c8b7b/5ae12c40N57c98ea8.jpg'), ('16675691362', 'https://img13.360buyimg.com/n7/jfs/t18490/21/2141098141/120513/b3ca521a/5ae90247N3b4909ae.jpg'), ('26222795271', 'https://img13.360buyimg.com/n7/jfs/t19441/291/1597121495/310550/9bc2e141/5ad05fc0N1510cae5.jpg'), ('1780924', 'https://img13.360buyimg.com/n7/jfs/t17167/97/1957869461/43204/d064647b/5adda3e0Ne1d3aa86.jpg'), ('4813030', 'https://img13.360buyimg.com/n7/jfs/t19198/83/1908967366/189260/7538e84b/5adda865N8f547981.jpg'), ('27036535156', 'https://img13.360buyimg.com/n7/jfs/t19399/140/2175516321/123017/41e6d6a8/5aea87d3N9736cc9d.jpg'), ('26348513019', 'https://img13.360buyimg.com/n7/jfs/t14857/240/2643838980/220943/c982fda1/5aaf2002Ndd25bc52.jpg'), ('26016197600', 'https://img13.360buyimg.com/n7/jfs/t19894/76/195725612/190103/23c60ca1/5aeabb94N3e0266bc.jpg'), ('25168000024', 'https://img13.360buyimg.com/n7/jfs/t17629/301/2062161127/434152/aa3560a5/5ae319f9N1ae1146c.jpg'), ('25965247088', 'https://img13.360buyimg.com/n7/jfs/t19270/67/2232771964/253207/25f41fd9/5aea61b0Nfd21a809.jpg'), ('10123099847', 'https://img13.360buyimg.com/n7/jfs/t15511/14/1469153129/729958/b0af0ca1/5a533063N15fea56c.jpg'), ('20000220615', 'https://img13.360buyimg.com/n7/jfs/t16426/172/2638358261/151693/87020840/5ab869ddN30621fec.jpg'), ('15904713681', 'https://img13.360buyimg.com/n7/jfs/t17287/197/2249621651/366556/d36ae213/5aeadb4cN97f413f3.jpg'), ('10114188069', 'https://img13.360buyimg.com/n7/jfs/t19927/88/179058964/386205/afd08ef1/5ae9717fN07f116d9.jpg'), ('10503200866', 'https://img13.360buyimg.com/n7/jfs/t18139/246/1628563908/114414/9315ac7c/5ad0647eNa9f1e2af.jpg'), ('1658610413', 'https://img13.360buyimg.com/n7/jfs/t19411/79/1017814440/108641/1b185d6d/5ab8b479Nd2417e97.jpg')]

正则匹配练习二:

如下nginx配置文件,按照需求分割成以配置文件名为名字的单独配置文件:

配置文件如下:

upstream orderCenter.ga10.wms5.jd.local {
    server 10.46.0.161:8023 weight=10 max_fails=2 fail_timeout=30s;
    server 10.46.0.162:8023 weight=10 max_fails=2 fail_timeout=30s;
}

upstream opperftrace.ga10.wms5.jd.local {
    server 10.46.0.164:8060 weight=10 max_fails=2 fail_timeout=30s;
}

upstream taskassign-c.ga10.wms5.jd.local {
    server 10.46.0.162:8005 weight=10 max_fails=2 fail_timeout=30s;
}

upstream smartQuery.ga10.wms5.jd.local {
    server 10.46.0.164:8013 weight=10 max_fails=2 fail_timeout=30s;
}

upstream center.ga10.wms5.jd.local {
    server 10.46.0.164:9020 weight=10 max_fails=2 fail_timeout=30s;
    server 10.46.0.163:9020 weight=10 max_fails=2 fail_timeout=30s;
}
upstream aps.wms5.jd.local {
  server 10.46.0.161:8001 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8001 weight=10 max_fails=2 fail_timeout=10s;
}
upstream inbound.wms5.jd.local {
  server 10.46.0.161:8002 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8002 weight=10 max_fails=2 fail_timeout=10s;
}
upstream invop.wms5.jd.local {
  server 10.46.0.161:8003 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8003 weight=10 max_fails=2 fail_timeout=10s;
}
upstream mcs.wms5.jd.local {
  server 10.46.0.161:8004 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8004 weight=10 max_fails=2 fail_timeout=10s;
}
upstream pickingplan.wms5.jd.local {
  server 10.46.0.161:8005 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8005 weight=10 max_fails=2 fail_timeout=10s;
}
upstream picking.wms5.jd.local {
  server 10.46.0.161:8006 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8006 weight=10 max_fails=2 fail_timeout=10s;
}
upstream power.wms5.jd.local {
  server 10.46.0.161:8007 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8007 weight=10 max_fails=2 fail_timeout=10s;
}
upstream shipment.wms5.jd.local {
  server 10.46.0.161:8008 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8008 weight=10 max_fails=2 fail_timeout=10s;
}
upstream stock.wms5.jd.local {
  server 10.46.0.161:8009 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8009 weight=10 max_fails=2 fail_timeout=10s;
}
upstream inner.wms5.jd.local {
  server 10.46.0.161:8011 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.162:8010 weight=10 max_fails=2 fail_timeout=10s;
}
upstream task.wms5.jd.local {
  server 10.46.0.163:8001 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.164:8001 weight=10 max_fails=2 fail_timeout=10s;
}
upstream taskmg.wms5.jd.local {
  server 10.46.0.163:8002 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.164:8002 weight=10 max_fails=2 fail_timeout=10s;
}
upstream report.wms5.jd.local {
  server 10.46.0.163:8003 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.164:8003 weight=10 max_fails=2 fail_timeout=10s;
}
upstream master.wms5.jd.local {
  server 10.46.0.163:8004 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.164:8004 weight=10 max_fails=2 fail_timeout=10s;
}
upstream clover.jd.local {
  server 10.46.0.163:1601 weight=10 max_fails=2 fail_timeout=10s;
  server 10.46.0.164:1601 weight=10 max_fails=2 fail_timeout=10s;
}

upstream backbone.web.wms5.jd.local {
  server 10.46.0.163:8006 weight=10 max_fails=2 fail_timeout=10s;
}
upstream wump-heartbeat.wms5.jd.local {
    server 10.46.0.130:8001 weight=10 max_fails=2 fail_timeout=10s;
}
upstream dec.wms5.jd.local {
        server 10.46.0.161:8012 weight=10 max_fails=2 fail_timeout=10s;
        server 10.46.0.162:8011 weight=10 max_fails=2 fail_timeout=10s;
}


# ##############################################################################

server
{
	listen                   80;
	server_name              ga10.wms5.jd.com 10.46.0.217 10.46.0.161;
	access_log               /export/servers/nginx/logs/ga10.wms5.jd.com/ga10.wms5.jd.com_access.log main;
	error_log                /export/servers/nginx/logs/ga10.wms5.jd.com/ga10.wms5.jd.com_error.log warn;
	#chunkin on;
	error_page 411 = @my_error; 
	location @my_error {
		#chunkin_resume;
	}


        location /logs/ {
                autoindex       off;
                deny all;
        }

	# frontend	########################################
	rewrite_log on;
	#more_set_headers "Foo: bar";
location /dec/ {
                proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
                proxy_set_header        Host  $host;
                proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                expires                 0;
                proxy_pass http://dec.wms5.jd.local/;
        }
location ~ /pickingplan/((?:services/)?taskassign_.+) {
    proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
    proxy_set_header        Host  $host;
    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    expires                 0;
    rewrite /pickingplan/((?:services/)?taskassign_.+) /$1 break;
    proxy_pass http://taskassign-c.ga10.wms5.jd.local;
}

location /wump-heartbeat/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://wump-heartbeat.wms5.jd.local/;
}
location /master/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://master.wms5.jd.local/;
}
location /mcs/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://mcs.wms5.jd.local/;
}
location /power/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://power.wms5.jd.local/;
}
location /cluster1/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://clover.jd.local;
}
location /aps/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://aps.wms5.jd.local/;
}
location /task/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://task.wms5.jd.local/;
}
location /taskmg/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://taskmg.wms5.jd.local/;
}
location /stock/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://stock.wms5.jd.local/;
}
location /invop/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://invop.wms5.jd.local/;
}
location /picking/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://picking.wms5.jd.local/;
}
location /pickingplan/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://pickingplan.wms5.jd.local/;
}
location /shipment/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://shipment.wms5.jd.local/;
}
location /inbound/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://inbound.wms5.jd.local/;
}
location /winapp/ {
	root /export/App/app.wms5.jd.local/;
	index ReleaseList.xml;
}	
location /rfapp/ {
	root /export/App/app.wms5.jd.local/;
	index ReleaseList.xml;	
}	
location /report/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://report.wms5.jd.local/;
}
location /inner/ {
	proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
	proxy_set_header        Host  $host;
	proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
	expires                 0;
	proxy_pass http://inner.wms5.jd.local/;
}
location /backbone/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://backbone.web.wms5.jd.local/;
}

    location /center/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://center.ga10.wms5.jd.local/;
    }

    location /smartQuery/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://smartQuery.ga10.wms5.jd.local/;
    }

    location /opperftrace/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://opperftrace.ga10.wms5.jd.local/;
    }

    location /orderCenter/ {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        expires                 0;
        proxy_pass http://orderCenter.ga10.wms5.jd.local/;
    }
}
步骤一:

使用正则匹配文件中的配置文件:

r"(upstream\s(\S+)\s{[^}]+})"

匹配后的数据:

mark
mark
步骤二:

先来读出内容并创建目录:

import codecs
import re
import os

reUpstream = re.compile(r"(upstream\s(\S+)\s{[^}]+})")

with codecs.open("configure.txt") as f_Upstream:        ##configure.txt就是如上的内容
    result = reUpstream.findall(f_Upstream.read())          ##读出文件内容
    if not os.path.exists("upstream"):
        os.mkdir("upstream")
    os.chdir("upstream")
    print(result)
    
输出结果:(多个元祖组成的列表)
[('upstream orderCenter.ga10.wms5.jd.local {\n    server 10.46.0.161:8023 weight=10 max_fails=2 fail_timeout=30s;\n    server 10.46.0.162:8023 weight=10 max_fails=2 fail_timeout=30s;\n}', 'orderCenter.ga10.wms5.jd.local'), ('upstream opperftrace.ga10.wms5.jd.local {\n    server 10.46.0.164:8060 weight=10 max_fails=2 fail_timeout=30s;\n}', 'opperftrace.ga10.wms5.jd.local'), ('upstream taskassign-c.ga10.wms5.jd.local {\n    server 10.46.0.162:8005 weight=10 max_fails=2 fail_timeout=30s;\n}', 'taskassign-c.ga10.wms5.jd.local'), ('upstream smartQuery.ga10.wms5.jd.local {\n    server 10.46.0.164:8013 weight=10 max_fails=2 fail_timeout=30s;\n}', 'smartQuery.ga10.wms5.jd.local'), ('upstream center.ga10.wms5.jd.local {\n    server 10.46.0.164:9020 weight=10 max_fails=2 fail_timeout=30s;\n    server 10.46.0.163:9020 weight=10 max_fails=2 fail_timeout=30s;\n}', 'center.ga10.wms5.jd.local'), ('upstream aps.wms5.jd.local {\n  server 10.46.0.161:8001 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.162:8001 weight=10 max_fails=2 fail_timeout=10s;\n}', 'aps.wms5.jd.local'), ('upstream inbound.wms5.jd.local {\n  server 10.46.0.161:8002 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.162:8002 weight=10 max_fails=2 fail_timeout=10s;\n}', 'inbound.wms5.jd.local'), ('upstream invop.wms5.jd.local {\n  server 10.46.0.161:8003 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.162:8003 weight=10 max_fails=2 fail_timeout=10s;\n}', 'invop.wms5.jd.local'), ('upstream mcs.wms5.jd.local {\n  server 10.46.0.161:8004 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.162:8004 weight=10 max_fails=2 fail_timeout=10s;\n}', 'mcs.wms5.jd.local'), ('upstream pickingplan.wms5.jd.local {\n  server 10.46.0.161:8005 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.162:8005 weight=10 max_fails=2 fail_timeout=10s;\n}', 'pickingplan.wms5.jd.local'), ('upstream picking.wms5.jd.local {\n  server 10.46.0.161:8006 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.162:8006 weight=10 max_fails=2 fail_timeout=10s;\n}', 'picking.wms5.jd.local'), ('upstream power.wms5.jd.local {\n  server 10.46.0.161:8007 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.162:8007 weight=10 max_fails=2 fail_timeout=10s;\n}', 'power.wms5.jd.local'), ('upstream shipment.wms5.jd.local {\n  server 10.46.0.161:8008 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.162:8008 weight=10 max_fails=2 fail_timeout=10s;\n}', 'shipment.wms5.jd.local'), ('upstream stock.wms5.jd.local {\n  server 10.46.0.161:8009 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.162:8009 weight=10 max_fails=2 fail_timeout=10s;\n}', 'stock.wms5.jd.local'), ('upstream inner.wms5.jd.local {\n  server 10.46.0.161:8011 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.162:8010 weight=10 max_fails=2 fail_timeout=10s;\n}', 'inner.wms5.jd.local'), ('upstream task.wms5.jd.local {\n  server 10.46.0.163:8001 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.164:8001 weight=10 max_fails=2 fail_timeout=10s;\n}', 'task.wms5.jd.local'), ('upstream taskmg.wms5.jd.local {\n  server 10.46.0.163:8002 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.164:8002 weight=10 max_fails=2 fail_timeout=10s;\n}', 'taskmg.wms5.jd.local'), ('upstream report.wms5.jd.local {\n  server 10.46.0.163:8003 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.164:8003 weight=10 max_fails=2 fail_timeout=10s;\n}', 'report.wms5.jd.local'), ('upstream master.wms5.jd.local {\n  server 10.46.0.163:8004 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.164:8004 weight=10 max_fails=2 fail_timeout=10s;\n}', 'master.wms5.jd.local'), ('upstream clover.jd.local {\n  server 10.46.0.163:1601 weight=10 max_fails=2 fail_timeout=10s;\n  server 10.46.0.164:1601 weight=10 max_fails=2 fail_timeout=10s;\n}', 'clover.jd.local'), ('upstream backbone.web.wms5.jd.local {\n  server 10.46.0.163:8006 weight=10 max_fails=2 fail_timeout=10s;\n}', 'backbone.web.wms5.jd.local'), ('upstream wump-heartbeat.wms5.jd.local {\n    server 10.46.0.130:8001 weight=10 max_fails=2 fail_timeout=10s;\n}', 'wump-heartbeat.wms5.jd.local'), ('upstream dec.wms5.jd.local {\n        server 10.46.0.161:8012 weight=10 max_fails=2 fail_timeout=10s;\n        server 10.46.0.162:8011 weight=10 max_fails=2 fail_timeout=10s;\n}', 'dec.wms5.jd.local')]
步骤三:

筛选upstream并写入各自的文件:

import codecs
import re
import os

reUpstream = re.compile(r"(upstream\s(\S+)\s{[^}]+})")

with codecs.open("configure.txt") as f_Upstream:
    result = reUpstream.findall(f_Upstream.read())
    if not os.path.exists("upstream"):
        os.mkdir("upstream")
    os.chdir("upstream")
    # print(result)
    for i in result:
        with codecs.open(i[1],"w") as w_Upstream:       ##i[1]为名称
            w_Upstream.write(i[0])          ##i[0]为内容
    os.chdir("..")

如下:其中i[1]为名称;i[0]为内容。

('upstream dec.wms5.jd.local {\n        server 10.46.0.161:8012 weight=10 max_fails=2 fail_timeout=10s;\n        server 10.46.0.162:8011 weight=10 max_fails=2 fail_timeout=10s;\n}', 'dec.wms5.jd.local')

最后生成的结果:

mark
mark

文件内的内容为:

mark
mark

正则匹配练习三:

继续接着如上的原文件,继续操作location:

步骤一:

编写正则匹配规则:

r"(location\s/(\S+)/\s{\s+[^}]+})"
步骤二:

匹配文件内容,并创建目录:

with codecs.open("configure.txt") as fl:
    rrr = reLocation.findall(fl.read())
    if not os.path.exists("location"):
        os.mkdir("location")
    os.chdir("location")
步骤三:
reLocation = re.compile(r"(location\s/(\S+)/\s{\s+[^}]+})")

with codecs.open("configure.txt") as fl:
    rrr = reLocation.findall(fl.read())
    if not os.path.exists("location"):
        os.mkdir("location")
    os.chdir("location")
    for j in rrr:
        with codecs.open(j[1],"w") as fff:
            fff.write(j[0])

最后形成的目录:

mark
mark

文件内容:

mark
mark
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/05/04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 正则匹配练习一:
  • 正则匹配练习二:
    • 步骤一:
      • 步骤二:
        • 步骤三:
        • 正则匹配练习三:
          • 步骤一:
            • 步骤二:
              • 步骤三:
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档