python3–正则匹配

正则匹配练习一:

给定一段字符串,利用 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)

输出:

输出结果为多个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{[^}]+})"

匹配后的数据:

步骤二:

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

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')

最后生成的结果:

文件内的内容为:

正则匹配练习三:

继续接着如上的原文件,继续操作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])

最后形成的目录:

文件内容:

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏AI科技大本营的专栏

视频教程 | 10行Python,搭建一个可以自动作曲的神经网络

程序员想转型AI却迟迟无法开始?也许你需要一种更加有趣的学习方式。 Siraj Raval 是一位人工智能领域的编程高手,毕业于哥伦比亚大学,曾任职于 Twi...

59870
来自专栏大数据-Hadoop、Spark

Ubuntu系统下Python关键字查看

>>> import keyword >>> keyword.kwlist ? ?

31360
来自专栏AI科技大本营的专栏

不会用Photoshop抠图?Mask R-CNN助你一键“除”人

翻译 | 林椿眄 编辑 | 费棋 【AI科技大本营导读】:看过英剧《黑镜》吗?圣诞特别版《白色圣诞节》中有这样一个场景:其中一个未来科技有自由屏蔽人像的功能,可...

48070
来自专栏AI科技大本营的专栏

视频教程 | 利用Python,用4分钟时间搭建一个情感分析系统

作者 | 谷磊 程序员想转型AI却迟迟无法开始?也许你需要一种更加有趣的学习方式。 Siraj Raval是一位人工智能领域的编程高手,毕业于哥伦比亚大学,曾...

47470
来自专栏AI科技大本营的专栏

Python爆红的5大理由,爱它没错!

Python 诞生之初就被誉为最容易上手的编程语言。进入火热的 AI 人工智能时代后,它也逐渐取代 Java,成为编程界的头牌语言。 另外Python 已经进入...

35670
来自专栏AI科技大本营的专栏

python语音识别终极指南

译者 | 廉洁 编辑 | 明明 【AI科技大本营导读】亚马逊的 Alexa 的巨大成功已经证明:在不远的将来,实现一定程度上的语音支持将成为日常科技的基本要求。...

89780
来自专栏AI科技大本营的专栏

视频教程【第5期】10行Python,搭建一个游戏人工智能

程序员想转型AI却迟迟无法开始?也许你需要一种更加有趣的学习方式。 Siraj Raval是一位人工智能领域的编程高手,毕业于哥伦比亚大学,曾任职于 Twili...

437130
来自专栏AI科技大本营的专栏

视频教程 | 7行Python,搭建一个可以识花的机器学习APP

程序员想转型AI却迟迟无法开始?也许你需要一种更加有趣的学习方式。 Siraj Raval是一位人工智能领域的编程高手,毕业于哥伦比亚大学,曾任职于 Twili...

44380
来自专栏AI科技大本营的专栏

Python爱好者周知:Scikit-Learn中文文档正式发布

整理 | 费棋 近日,Scikit-Learn 中文文档已由开源组织 ApacheCN 完成校对。该中文文档依然包含了 Scikit-Learn 基本功能的六大...

29860
来自专栏AI科技大本营的专栏

视频教程 | 如何用Python搭建可以画风迁移的人工智能

艺术源于生活,它诠释了我们在生活中的经历与情感。那么我们能搭建一个AI艺术家吗?答案是肯定的。这听起来很不可思议,我们可以使用一个艺术家的风格来训练出一个神经网...

41060

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励