Python3之正则表达式实例

在线测试正则表达式网站:https://regex101.com/ 

223758_95dg_3804957.png

 

1.测试一:

测试文本内容:

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

  正则匹配:a+1+b+c+\W+\s+[a-zA-Z]+\W[a-z]+

匹配结果:

211836_u5nJ_3804957.png
 

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

生成结果如下:

221308_1gqq_3804957.png 

upstream内:

225135_y1G7_3804957.png

location内:

225232_xkZD_3804957.png

224244_umVU_3804957.png

224839_yChq_3804957.png

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

 

转载于:https://my.oschina.net/u/3804957/blog/1807073

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值