今天主要是以2道习题的为主,加强对正则表达式的理解:
一,获取json中的skuid和skuimgurl的值:
"errCode":"0",
"retCode":"",
"msg":"success",
"size":"20",
"sku":[
{
"skuid":"26878432382",
"spuid":"26878432382",
"skuurl":"[https://qitem.jd.com/fcgi-bin/view?sku=26878432382&shopid=0&enc=jn6CsRbAaCZmUGvwesX4UtzRM1_Urt4t3rrpcBqQFCaOb20E9H_1lIDZrnjpTnQxpDyV61PjwLJntzDdBK6Ws0stJnT97ZXn5YoMHMfdHIXg551U4dquFQ%3D%3D&tk=b532cffc1b0581ce0b3da372aacc8c1d&share=1&fs=1>=1525352327&spu=26878432382&v=1](https://qitem.jd.com/fcgi-bin/view?sku=26878432382&shopid=0&enc=jn6CsRbAaCZmUGvwesX4UtzRM1_Urt4t3rrpcBqQFCaOb20E9H_1lIDZrnjpTnQxpDyV61PjwLJntzDdBK6Ws0stJnT97ZXn5YoMHMfdHIXg551U4dquFQ%3D%3D&tk=b532cffc1b0581ce0b3da372aacc8c1d&share=1&fs=1%3E=1525352327&spu=26878432382&v=1)",
"skuimgurl":"[https://img12.360buyimg.com/n7/jfs/t18226/169/1318243724/390477/5b0718ff/5ac44edcNa350dbd9.jpg](https://img12.360buyimg.com/n7/jfs/t18226/169/1318243724/390477/5b0718ff/5ac44edcNa350dbd9.jpg)",
"title":"丹龙(DanLong)声波电动牙刷成人家用非充电式智能防水情侣牙刷 牛奶白-标配",
"price":"129.00",
"commissionprice":"25.80",
"salecount":"6551",
"goodCom":"3031",
"comRate":"0.2000"
},
...
image.png
首先,我们在线https://regex101.com/
对正则表达式检测:如图
获取我们需要的正则表达式
\"skuid\S{3}(\d+)\S.*?skuimgurl\S{3}(.*?jpg)\"
image.png
然后使用
findall()
方法得到匹配成功的list;
二,对文件内容匹配并相对应的生成文件:
有一个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;
}
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/;
}
}
(1) 获取upstream后面的内容 相对应的生成文件,将其包含的内容写入文件中:
import codecs
import re
import os
# 匹配出文件名的正则表达式
'''
第一个()匹配出upstream的所有内容: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后面的文件名aps.wms5.jd.local
'''
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"):
os.mkdir("upstream") # 不存在就创建upstreamwen文件夹
os.chdir("upstream") # 切换到新建文件夹下
for item in textList:
with codecs.open(item[1],"w") as fw:
fw.write(item[0])
os.chdir("..")
运行结果:
image.png
网友评论