【问题标题】:Python regex with brackets带括号的 Python 正则表达式
【发布时间】:2021-12-14 23:03:31
【问题描述】:

我正在尝试获取如下字符串的子集:

CUSTOMER1 => {
[
    [
        SOME_CONTENT_1
    ],
    [
        SOME_CONTENT_2
    ],
    [
        SOME_CONTENT_3
    ]
]
}
CUSTOMER2 => {
[
    [
        SOME_CONTENT_4
    ],
    [
        SOME_CONTENT_5
    ],
    [
        SOME_CONTENT_6
    ]
]
}

我需要先捕获 { 和 } 之间的内容,然后将每个 SOME_CONTENT 放入数组的单独索引中。 我现在拥有的是:

{\s*\n(.+(?:\n*.+)*)} 

但它没有按预期工作。 我的预期结果如下所示:

[[CUSTOMER1, SOME_CONTENT_1, SOME_CONTENT_2, SOME_CONTENT_3],[CUSTOMER2, SOME_CONTENT_4, SOME_CONTENT_5, SOME_CONTENT_6]]

感谢任何帮助。

更新: 请考虑以下作为输入字符串:

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
]
]
}

预期输出:

[["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"]

【问题讨论】:

  • 您是否试图将预期的输出作为一个列表来进行迭代,等等?还是你想要它作为一个文字字符串?

标签: python regex


【解决方案1】:

假设你想得到一个列表的输出,你能试试吗:

#!/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)

输出:

[['CUSTOMER1', 'SOME_CONTENT_1', 'SOME_CONTENT_2', 'SOME_CONTENT_3'], ['CUSTOMER2', 'SOME_CONTENT_4', 'SOME_CONTENT_5', 'SOME_CONTENT_6']]

[更新]
至于更新后的输入,请您试一试(假设变量s分配给输入):

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)

【讨论】:

  • 你的答案是正确的。能否请您根据问题 UPDATE 修改它?
  • 感谢您的反馈。您能否提供更新的预期输出?整个结果是不必要的。只需几个元素就足够了。我的主要兴趣是如何处理字符串中的冒号和空格。干杯。
  • 感谢您的更新。但输出与 my 的预期不同。它看起来像一个格式化的纯文本,而不是一个 python 列表。
  • 我已经更新了我的答案。我不确定它的输出是否符合您的要求。这是一个起点,欢迎您提供反馈。
  • 很高兴知道这一点。干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
相关资源
最近更新 更多