Regular expression example of Python3

Online testing regular expression website: https://regex101.com/ 

 

1. Test one:

Test text content:

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

  Matching: a + 1 + b + c + \ W + \ s + [a-zA-Z] + \ W [az] +

Match result:


 

2. Test two

Test file ga10.wms5.jd.com.txt content:

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

Extract the contents of the above file, generate two folders (upstream, location), and generate corresponding configuration files in each folder. The file name is the name after upstream, such as: orderCenter.ga10.wms5.jd.local, and the contents of upstream{ ...} are written into this file orderCenter.ga10.wms5.jd.local respectively.

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

The result is as follows:

 

In upstream:

Within the location:

The python code is as follows:

#!/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])

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325358239&siteId=291194637