在使用re.finditer()和re.findall()与输出进行匹配时看到奇怪的行为,它仅返回最后一个匹配项,我怀疑[\s\S]+
与它有关,但是没有如果匹配项无法捕获任何内容:
tre=re.compile(r'''
Name:\s+(?P<NAME>.*)\s+
Destination:\s+(?P<DESTINATION>.*)\s+Ifhandle[\s\S]+
Signalled-Name:\s+(?P<SIGNAME>.*$)[\s\S]+
Admin:\s+(?P<ADMIN>\w+)\s+Oper:\s+(?P<OPER>\w+)\s+Path:
''', re.M | re.VERBOSE)
m = [m.tremditer(out)中m的m.groupdict()]
m
[{'ADMIN':'up','OPER':'up','DESTINATION':'10 .10.10.4','NAME':'tunnel-te4','SIGNAME':'BKLN-to- AGN6'}]
此示例使用的输出。我已经在活动节点上对此进行了测试,并看到了相同的结果。
out='''Name: tunnel-te4 Destination: 10.10.10.4 Ifhandle:0x90
Signalled-Name: BKLN-to-AGN4
Status:
Admin: up Oper: up Path: valid Signalling: connected
path option 100, type dynamic (Basis for Setup, path weight 60)
G-PID: 0x0800 (derived from egress interface properties)
Bandwidth Requested: 1000 kbps CT0
Creation Time: Wed Jul 12 10:30:58 2017 (19w4d ago)
Config Parameters:
Bandwidth: 1000 kbps (CT0) Priority: 7 7 Affinity: 0x0/0x2
Metric Type: TE (global)
Path Selection:
Tiebreaker: Min-fill (default)
Hop-limit: disabled
Cost-limit: disabled
Path-invalidation timeout: 10000 msec (default), Action: Tear (default)
AutoRoute: disabled LockDown: disabled Policy class: not set
Forward class: 0 (default)
Forwarding-Adjacency: disabled
Autoroute Destinations: 0
Loadshare: 0 equal loadshares
Auto-bw: disabled
Fast Reroute: Disabled, Protection Desired: None
Path Protection: Not Enabled
BFD Fast Detection: Disabled
Reoptimization after affinity failure: Enabled
Soft Preemption: Disabled
History:
Tunnel has been up for: 6d18h (since Mon Nov 20 10:15:45 UTC 2017)
Current LSP:
Uptime: 6d18h (since Mon Nov 20 10:15:45 UTC 2017)
Reopt. LSP:
Last Failure:
LSP not signalled, identical to the [CURRENT] LSP
Date/Time: Mon Nov 27 03:45:31 UTC 2017 [01:11:28 ago]
Prior LSP:
ID: 2 Path Option: 100
Removal Trigger: path verification failed
Path info (IS-IS A002 level-2):
Node hop count: 2
Hop0: 10.10.2.9
Hop1: 10.10.2.5
Hop2: 10.10.10.4
Name: tunnel-te5 Destination: 10.10.10.5 Ifhandle:0xb0
Signalled-Name: BKLN-to-AGN5
Status:
Admin: up Oper: up Path: valid Signalling: connected
path option 100, type dynamic (Basis for Setup, path weight 30)
G-PID: 0x0800 (derived from egress interface properties)
Bandwidth Requested: 1000 kbps CT0
Creation Time: Wed Jul 12 10:30:58 2017 (19w4d ago)
Config Parameters:
Bandwidth: 1000 kbps (CT0) Priority: 7 7 Affinity: 0x0/0x2
Metric Type: TE (global)
Path Selection:
Tiebreaker: Min-fill (default)
Hop-limit: disabled
Cost-limit: disabled
Path-invalidation timeout: 10000 msec (default), Action: Tear (default)
AutoRoute: disabled LockDown: disabled Policy class: not set
Forward class: 0 (default)
Forwarding-Adjacency: disabled
Autoroute Destinations: 0
Loadshare: 0 equal loadshares
Auto-bw: disabled
Fast Reroute: Disabled, Protection Desired: None
Path Protection: Not Enabled
BFD Fast Detection: Disabled
Reoptimization after affinity failure: Enabled
Soft Preemption: Disabled
History:
Tunnel has been up for: 6d18h (since Mon Nov 20 10:15:32 UTC 2017)
Current LSP:
Uptime: 6d18h (since Mon Nov 20 10:15:32 UTC 2017)
Reopt. LSP:
Last Failure:
LSP not signalled, identical to the [CURRENT] LSP
Date/Time: Mon Nov 27 03:45:31 UTC 2017 [01:11:28 ago]
Prior LSP:
ID: 2 Path Option: 100
Removal Trigger: path verification failed
Path info (IS-IS A002 level-2):
Node hop count: 1
Hop0: 10.10.2.9
Hop1: 10.10.10.5
Name: tunnel-te6 Destination: 10.10.10.6 Ifhandle:0xd0
Signalled-Name: BKLN-to-AGN6
Status:
Admin: up Oper: up Path: valid Signalling: connected
path option 100, type dynamic (Basis for Setup, path weight 90)
G-PID: 0x0800 (derived from egress interface properties)
Bandwidth Requested: 1000 kbps CT0
Creation Time: Wed Jul 12 10:30:58 2017 (19w4d ago)
Config Parameters:
Bandwidth: 1000 kbps (CT0) Priority: 7 7 Affinity: 0x0/0x2
Metric Type: TE (global)
Path Selection:
Tiebreaker: Min-fill (default)
Hop-limit: disabled
Cost-limit: disabled
Path-invalidation timeout: 10000 msec (default), Action: Tear (default)
AutoRoute: disabled LockDown: disabled Policy class: not set
Forward class: 0 (default)
Forwarding-Adjacency: disabled
Autoroute Destinations: 0
Loadshare: 0 eq’’’