0

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"]
1
  • Are you trying to get your expected output as a list so that you can iterate over it, etc.? Or do you want it as a literal string? Commented Dec 15, 2021 at 5:43

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

8 Comments

your answer is correct. could you please modify it according to the question UPDATE?
Thank for the feedback. Would you please provide the expected output for the update? The whole result is not necessary. Just a few elements will be good enough. My main interest is how I should handle the colons and whitespaces in the strings. Cheers.
Thank you for the update. But the output differs from my expectation. It looks like a formatted plain text rather than a python list.
I've updated my answer. I'm not sure its output meets your requirement. This is a staring point and your feedback is welcome.
Good to know that. Cheers.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.