在线测试正则表达式网站:https://regex101.com/
1.测试一:
测试文本内容:
aaaa11111bbbbbccccc#$%^^
adfal;ksdfkl2kl135jn sdgf
asd
fa
sdf\hg\rhy3e562ojgasdfkl;ga
fasdfjk
正则匹配:a+1+b+c+\W+\s+[a-zA-Z]+\W[a-z]+
匹配结果:
2.测试二
测试文件ga10.wms5.jd.com.txt内容:
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内:
location内:
python代码如下:
#!/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])