I'm trying to get a subset of strings like bellow:
CUSTOMER1 => {
[
[
SOME_CONTENT_1
],
[
SOME_CONTENT_2
],
[
SOME_CONTENT_3
]
]
}
CUSTOMER2 => {
[
[
SOME_CONTENT_4
],
[
SOME_CONTENT_5
],
[
SOME_CONTENT_6
]
]
}
I need to first capture contents between { and } and then put each of SOME_CONTENT in the separate index of an array. What I have till now is :
{\s*\n(. (?:\n*. )*)}
But it does not work as expected. My expected result is something like bellow:
[[CUSTOMER1, SOME_CONTENT_1, SOME_CONTENT_2, SOME_CONTENT_3],[CUSTOMER2, SOME_CONTENT_4, SOME_CONTENT_5, SOME_CONTENT_6]]
Any help is appreciated.
UPDATE: Please consider the following as an input string:
ok: [router2] => {
output.stdout_lines: [
[
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone,
D - Remote, C - CVTA, M - Two-port Mac Relay
Device ID Local Intrfce Holdtme Capability Platform Port ID
R1.example.com Gig 4/0 136 R 7206VXR Gig 3/0
SW2.example.com Gig 3/0 153 R S I Linux Uni Eth 2/0
],
[
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 unassigned YES NVRAM administratively down down
GigabitEthernet1/0 unassigned YES NVRAM administratively down down
GigabitEthernet2/0 unassigned YES NVRAM administratively down down
GigabitEthernet3/0 192.168.61.101 YES NVRAM up up
GigabitEthernet4/0 unassigned YES NVRAM up up
FastEthernet5/0 unassigned YES NVRAM administratively down down
FastEthernet5/1 unassigned YES NVRAM administratively down down
FastEthernet6/0 unassigned YES NVRAM administratively down down
FastEthernet6/1 unassigned YES NVRAM administratively down down
],
[
EIGRP-IPv4 Neighbors for AS(100)
H Address Interface Hold Uptime SRTT RTO Q Seq
(sec) (ms) Cnt Num
0 192.168.61.100 Gi3/0 13 02:40:40 24 144 0 135
]
]
}
ok: [router1] => {
output.stdout_lines: [
[
Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone,
D - Remote, C - CVTA, M - Two-port Mac Relay
Device ID Local Intrfce Holdtme Capability Platform Port ID
R2.example.com Gig 3/0 164 R 7206VXR Gig 4/0
SW1.example.com Gig 1/0 130 R S I Linux Uni Eth 1/3
],
[
Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 unassigned YES NVRAM administratively down down
GigabitEthernet1/0 192.168.61.100 YES NVRAM up up
GigabitEthernet2/0 unassigned YES NVRAM administratively down down
GigabitEthernet3/0 unassigned YES NVRAM up up
GigabitEthernet4/0 unassigned YES NVRAM administratively down down
FastEthernet5/0 unassigned YES NVRAM administratively down down
FastEthernet5/1 unassigned YES NVRAM administratively down down
FastEthernet6/0 unassigned YES NVRAM administratively down down
FastEthernet6/1 unassigned YES NVRAM administratively down down
],
[
EIGRP-IPv4 Neighbors for AS(100)
H Address Interface Hold Uptime SRTT RTO Q Seq
(sec) (ms) Cnt Num
0 192.168.61.101 Gi1/0 13 02:40:41 340 2040 0 135
]
]
}
Expected output:
[["router2","Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone,
D - Remote, C - CVTA, M - Two-port Mac Relay
Device ID Local Intrfce Holdtme Capability Platform Port ID
R1.example.com Gig 4/0 136 R 7206VXR Gig 3/0
SW2.example.com Gig 3/0 153 R S I Linux Uni Eth 2/0" , " Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 unassigned YES NVRAM administratively down down
GigabitEthernet1/0 unassigned YES NVRAM administratively down down
GigabitEthernet2/0 unassigned YES NVRAM administratively down down
GigabitEthernet3/0 192.168.61.101 YES NVRAM up up
GigabitEthernet4/0 unassigned YES NVRAM up up
FastEthernet5/0 unassigned YES NVRAM administratively down down
FastEthernet5/1 unassigned YES NVRAM administratively down down
FastEthernet6/0 unassigned YES NVRAM administratively down down
FastEthernet6/1 unassigned YES NVRAM administratively down down", " EIGRP-IPv4 Neighbors for AS(100)
H Address Interface Hold Uptime SRTT RTO Q Seq
(sec) (ms) Cnt Num
0 192.168.61.100 Gi3/0 13 02:40:40 24 144 0 135"], ["router1", "Capability Codes: R - Router, T - Trans Bridge, B - Source Route Bridge
S - Switch, H - Host, I - IGMP, r - Repeater, P - Phone,
D - Remote, C - CVTA, M - Two-port Mac Relay
Device ID Local Intrfce Holdtme Capability Platform Port ID
R2.example.com Gig 3/0 164 R 7206VXR Gig 4/0
SW1.example.com Gig 1/0 130 R S I Linux Uni Eth 1/3", "Interface IP-Address OK? Method Status Protocol
FastEthernet0/0 unassigned YES NVRAM administratively down down
GigabitEthernet1/0 192.168.61.100 YES NVRAM up up
GigabitEthernet2/0 unassigned YES NVRAM administratively down down
GigabitEthernet3/0 unassigned YES NVRAM up up
GigabitEthernet4/0 unassigned YES NVRAM administratively down down
FastEthernet5/0 unassigned YES NVRAM administratively down down
FastEthernet5/1 unassigned YES NVRAM administratively down down
FastEthernet6/0 unassigned YES NVRAM administratively down down
FastEthernet6/1 unassigned YES NVRAM administratively down down", " EIGRP-IPv4 Neighbors for AS(100)
H Address Interface Hold Uptime SRTT RTO Q Seq
(sec) (ms) Cnt Num
0 192.168.61.101 Gi1/0 13 02:40:41 340 2040 0 135"]
CodePudding user response:
Assuming you want to get the output as a list, would you please try:
#!/usr/bin/python
import re
s = '''
CUSTOMER1 => {
[
[
SOME_CONTENT_1
],
[
SOME_CONTENT_2
],
[
SOME_CONTENT_3
]
]
}
CUSTOMER2 => {
[
[
SOME_CONTENT_4
],
[
SOME_CONTENT_5
],
[
SOME_CONTENT_6
]
]
}
'''
l1 = []
for k, v in re.findall(r'(\w )\s*=>\s*\{\s*\[(. ?)]\s*}', s, re.DOTALL):
l2 = [k]
for m in re.findall(r'\s*\[\s*(\w )\s*]', v):
l2.append(m)
l1.append(l2)
print(l1)
Output:
[['CUSTOMER1', 'SOME_CONTENT_1', 'SOME_CONTENT_2', 'SOME_CONTENT_3'], ['CUSTOMER2', 'SOME_CONTENT_4', 'SOME_CONTENT_5', 'SOME_CONTENT_6']]
[UPDATE]
As for the updated input, would you please try (assuming the variable s
is assigned to the input):
for k, v in re.findall(r'.*?\[(. ?)]\s*=>\s*\{.*?\[(. ?)]\s*}', s, re.DOTALL):
l2 = [k]
for m in re.findall(r'\[(. ?)]', v, re.DOTALL):
l2.append(m.strip())
l1.append(l2)
print(l1)