Python re模块
1、匹配中文
# u'[\u4e00-\u9fa5]+'(匹配所有中文的unicode类型) text='中文ABCD测试12345测试' prog = re.compile(u'[\u4e00-\u9fa5]+\w+[\u4e00-\u9fa5]+') res=prog.findall(text) print(res)
2、分组匹配
# !/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = import re text=""" terminal length 0 RuiJie#S6510-48VS8CQ-01#show vxlan mac local Vxlan MAC Address Type Location Interface Vlan Live Time ---------- -------------------- -------- -------- -------------------------------------- ------ ----------------- 61009 4c00.0ab6.0301 DYNAMIC LOCAL AggregatePort 14 1011 125d 23:07:34 61009 4c00.0ab6.0302 DYNAMIC LOCAL AggregatePort 16 1011 125d 22:09:00 61011 f0ff.a9f8.70f3 DYNAMIC LOCAL AggregatePort 20 1010 125d 23:07:34 61011 f0ff.a9f8.bac3 DYNAMIC LOCAL AggregatePort 21 1010 125d 23:07:34 61011 f0ff.a9f8.bd65 DYNAMIC LOCAL AggregatePort 15 1010 125d 21:36:57 61011 f0ff.a9f8.c2a1 MLAG LOCAL AggregatePort 16 1010 127d 02:24:52 61011 f0ff.a9f8.c6ef MLAG LOCAL AggregatePort 19 1010 127d 02:13:05 61013 4c00.0ab6.0701 MLAG LOCAL AggregatePort 15 1013 6d 15:21:45 61013 4c00.0ab6.0702 DYNAMIC LOCAL AggregatePort 15 1013 125d 21:30:41 61013 4c00.0ab6.0703 DYNAMIC LOCAL AggregatePort 14 1013 125d 23:07:34 61013 4c00.0ab6.0704 DYNAMIC LOCAL AggregatePort 16 1013 6d 15:21:44 61013 4c00.0ab6.0705 DYNAMIC LOCAL AggregatePort 14 1013 125d 23:07:34 61014 4c00.0ab6.0a01 MLAG LOCAL AggregatePort 19 1016 0d 00:06:48 61014 4c00.0ab6.0a02 DYNAMIC LOCAL AggregatePort 19 1016 125d 23:07:34 61014 4c00.0ab6.0a03 MLAG LOCAL AggregatePort 19 1016 0d 00:06:48 61014 4c00.0ab6.0a04 DYNAMIC LOCAL AggregatePort 19 1016 0d 00:01:53 61014 4c00.0ab6.0a05 MLAG LOCAL AggregatePort 18 1016 6d 15:26:36 RuiJie#S6510-48VS8CQ-01# """ pattern = re.compile(r'(?P<mac>[a-z0-9]+\.[a-z0-9]+\.[a-z0-9]+).*(?P<interface>AggregatePort\s+[0-9]+)') res = pattern.finditer(text) maclist = [] for i in res: """ i.groupdict():把匹配的结果转换为字典 """ macdict = {} macdict['mac'] = i.groupdict()['mac'] macdict['interface'] = i.groupdict()['interface'].replace('AggregatePort ', 'TFGigabitEthernet 0/') maclist.append(macdict) print(maclist)
3、防火墙配置格式化
# !/usr/bin/env python # -*- coding:utf-8 -*- # __author__ = import re content = """ security-policy ip rule 0 name icmp action pass counting enable service ping rule 1 name outbond action pass counting enable source-zone Trust destination-zone Untrust rule 2 name test action pass counting enable source-zone Untrust destination-zone Trust source-ip test service ssh rule 3 name NET-NWCL-20230113 action pass counting enable source-zone Untrust destination-zone Trust source-ip 10.180.0.7 destination-ip 10.176.0.1-3 service 6443 rule 4 name KFCSY_ceshi action pass counting enable source-zone Untrust destination-zone Trust source-ip ceshi_ip destination-ip ceshi_ip rule 5 name NET-NWCL-20230612 action pass counting enable source-zone Untrust destination-zone Trust source-ip-subnet 10.10.10.0 255.255.255.0 destination-ip-host 10.10.20.10 service ssh rule 6 name NET-NWCL-20230613 action pass counting enable source-zone Untrust destination-zone Trust source-ip-range 10.10.10.0 255.255.255.0 destination-ip-range 10.10.20.10 # security-policy ipv6 rule 0 name icmpv6 action pass counting enable service pingv6 # """ pattern = re.compile( r'(?P<rule>rule.*)\n\s+(?P<action>action.*)\n\s+(?P<counting>counting.*)\n\s+(?P<szone>source-zone.*)\n\s+(?P<dzone>destination-zone.*)\n\s+(?P<source>source-ip.*)\n\s+(?P<destination>destination-ip.*)\n(?P<service>\s+service.*|(?##|\s+))') res = pattern.finditer(content) for i in res: print(i.groupdict())
参考链接:
https://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html
https://www.cnblogs.com/shenjianping/p/11647473.html
https://docs.python.org/zh-cn/3.8/library/index.html #Python 标准库
https://docs.python.org/zh-cn/3.10/library/re.html
https://www.cnblogs.com/Eva-J/articles/7228075.html#_label7