【发布时间】: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"]
【问题讨论】:
-
您是否试图将预期的输出作为一个列表来进行迭代,等等?还是你想要它作为一个文字字符串?