Python3之正則表達式實例

在線測試正則表達式網站:https://regex101.com/ python

 

1.測試一:

測試文本內容:nginx

aaaa11111bbbbbccccc#$%^^ 
adfal;ksdfkl2kl135jn sdgf
asd
fa
sdf\hg\rhy3e562ojgasdfkl;ga
fasdfjk

  正則匹配:a+1+b+c+\W+\s+[a-zA-Z]+\W[a-z]+正則表達式

匹配結果:c#


 

2.測試二

測試文件ga10.wms5.jd.com.txt內容:frontend

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;
}



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

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;
}


}

把上面文件中的內容提取出來,生成兩個文件夾(upstream、location),每一個文件夾內生成對應配置文件。文件名爲upstream後的名字, 如:orderCenter.ga10.wms5.jd.local ,把upstream{ ...}內容分別寫入到此文件orderCenter.ga10.wms5.jd.local 中。測試

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內:spa

location內:code

python代碼以下:server

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/5/4 22:30
# @Author  : Feng Xiaoqing
# @File    : demon2.py
import codecs
import re
import os


#下面生成upstream內容
regUpstream = re.compile(r"\s*(upstream\s+(\S+)\s+{[^}]+})") #正則匹配
with codecs.open("ga10.wms5.jd.com.txt") as fu:   
    textList = regUpstream.findall(fu.read())
    if not os.path.exists("upstream"):            #判斷upstream文件夾是否存在,不存在則創建
        os.mkdir("upstream")                  
    os.chdir("upstream")
    for item in textList:
        with codecs.open(item[1], "w") as fw:     #生成以item[1]爲名字的文件,並把匹配到的內容寫入文件中
            fw.write(item[0])
    os.chdir("..")


#下面生成location內容
regLocation = re.compile(r"(location\s+/(\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"):            #判斷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])
相關文章
相關標籤/搜索