Python Regex findall()返回空键

yhxst69z  于 2023-08-08  发布在  Python
关注(0)|答案(1)|浏览(96)

我试图用正则表达式findall()捕获文本中的几个组消息,但我总是在捕获中得到空值,如果我使用每个正则表达式模式分开,这些值就不存在了,所以我怀疑findall()是这种行为的根本原因:

输入文本:

ACCESSPOINT01#show inter dot 0
Load for 1 min: 1.06, 5 min: 1.12, 15 min: 1.13
Date: *05:29:05 UTC Mon Jul 24 2023

Dot11Radio0 is UP, line protocol is UP
Hardware is 802.11 2.4G Radio, channel is 11
Radio MAC is F4:xx:yy:zz:1E:40

Dot11Radio0     Link encap:Ethernet  HWaddr F4:xx:yy:zz:1E:40  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:88943849 errors:0 dropped:189 overruns:0 frame:0
          TX packets:4471475 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:294364122 (280.7 MiB)  TX bytes:336905236 (321.2 MiB)
          Interrupt:65 Memory:f8000000-f8200000 

        DOT11 Statistics (Cumulative Total/Last 5 Seconds):
RECEIVER                                TRANSMITTER
Host Rx K Bytes:         20931/0        Host Tx K Bytes:        188148/0
Unicasts Rx:            113976/0        Unicasts Tx:            189770/0
Broadcasts Rx:             253/0        Broadcasts Tx:               0/0
Beacons Rx:           69666326/117      Beacons Tx:           85507736/98
Probes Rx:             4115902/4        Probes Tx:             1075910/0
Multicasts Rx:            1744/0        Multicasts Tx:               0/0
Mgmt Packets Rx:      73769674/121      Mgmt Packets Tx:           350/0
Ctrl Frames Rx:        4060958/0        Ctrl Frames Tx:          79794/0
RTS received:           742661/0        RTS transmitted:         20579/0
Duplicate frames:            0/0        CTS not received:         6662/0
MIC errors:                  0/0        WEP errors:                  0/0
FCS errors:           13657410/5        Retries:                 30434/0
Key Index errors:         8161/0        Tx Failures:             17482/0
                                        Tx Drops:                  100/0

Rate Statistics for Radio::
[Legacy]:
6 Mbps:
 Rx Packets:        210/0            Tx Packets:          0/0
                                     Tx Retries:         69/0
18 Mbps:
 Rx Packets:        107/0            Tx Packets:          0/0
                                     Tx Retries:          0/0
24 Mbps:
 Rx Packets:        129/0            Tx Packets:        127/0
                                     Tx Retries:         18/0
1 Mbps:
 Rx Packets:        104/0            Tx Packets:          0/0
                                     Tx Retries:          0/0
5.5 Mbps:
 Rx Packets:         53/0            Tx Packets:          0/0
                                     Tx Retries:          0/0

[SU]:
M0:
 Rx Packets:        307/0            Tx Packets:          0/0
                                     Tx Retries:          0/0
M1:
 Rx Packets:         88/0            Tx Packets:         41/0
                                     Tx Retries:         54/0
M2:
 Rx Packets:        204/0            Tx Packets:         28/0
                                     Tx Retries:         62/0
M3:
 Rx Packets:        827/0            Tx Packets:        189/0
                                     Tx Retries:        175/0
M4:
 Rx Packets:       1821/0            Tx Packets:       1310/0
                                     Tx Retries:        620/0
M5:
 Rx Packets:       1163/0            Tx Packets:       5219/0
                                     Tx Retries:       2299/0
M6:
 Rx Packets:       2589/0            Tx Packets:      28293/0
                                     Tx Retries:       6406/0
M7:
 Rx Packets:     108325/0            Tx Packets:     154563/0
                                     Tx Retries:      20731/0


Beacons missed: 0-30s 31-60s 61-90s 90s+
                     0      0      0    0

Vap TX statistics:
vap      ssid  MGMT      Beacon     DATA     [BK      BE  VI   VO] Bytes(Data) QosRetries AMDPU-SubFrameRetries
  2  GuestNet 347/0 42751408/48 189760/0 99145/0 90498/0 0/0 117/0 192659307/0    30432/0                   0/0
  3 DeviceNet   3/0 42756424/48     10/0     0/0     0/0 0/0  10/0      4329/0        2/0                   0/0

Vap RX statistics:
vap      ssid  MGMT     DATA   [BK       BE    VI  VO] Bytes(Data)
  2  GuestNet 543/0 115927/0 151/0 113362/0 422/0 52/0  21432778/0
  3 DeviceNet  18/0      9/0   0/0      0/0   0/0  9/0       637/0



ACCESSPOINT01#show inter dot 1
Load for 1 min: 1.05, 5 min: 1.12, 15 min: 1.13
Date: *05:29:08 UTC Mon Jul 24 2023

Dot11Radio1 is UP, line protocol is UP
Hardware is 802.11 5.0G Radio, channel is 64
Radio MAC is F4:xx:yy:zz:1E:40

Dot11Radio1     Link encap:Ethernet  HWaddr F4:xx:yy:zz:1E:40  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:114784169 errors:0 dropped:835 overruns:0 frame:0
          TX packets:94799029 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:3284042594 (3.0 GiB)  TX bytes:3559168106 (3.3 GiB)
          Interrupt:102 Memory:f8400000-f8600000 

        DOT11 Statistics (Cumulative Total/Last 5 Seconds):
RECEIVER                                TRANSMITTER
Host Rx K Bytes:      79607258/0        Host Tx K Bytes:     101832626/0
Unicasts Rx:          73715624/0        Unicasts Tx:          96438538/0
Broadcasts Rx:           28734/0        Broadcasts Tx:         1652718/5
Beacons Rx:             203978/0        Beacons Tx:          128198404/144
Probes Rx:              388593/0        Probes Tx:              824009/0
Multicasts Rx:          313967/0        Multicasts Tx:         5525078/8
Mgmt Packets Rx:       1542440/0        Mgmt Packets Tx:        146786/4
Ctrl Frames Rx:       66598283/0        Ctrl Frames Tx:       49372271/0
RTS received:          9451009/0        RTS transmitted:      14920090/0
Duplicate frames:            0/0        CTS not received:       998703/0
MIC errors:                  0/0        WEP errors:                  0/0
FCS errors:             389832/0        Retries:              35360788/0
Key Index errors:       128994/0        Tx Failures:          23306795/0
                                        Tx Drops:               209869/0

Rate Statistics for Radio::
[Legacy]:
6 Mbps:
 Rx Packets:      26080/0            Tx Packets:          2/0
                                     Tx Retries:       1093/0
18 Mbps:
 Rx Packets:        704/0            Tx Packets:          0/0
                                     Tx Retries:          0/0
24 Mbps:
 Rx Packets:       2141/0            Tx Packets:       4320/0
                                     Tx Retries:        165/0
36 Mbps:
 Rx Packets:        554/0            Tx Packets:          0/0
                                     Tx Retries:          0/0
48 Mbps:
 Rx Packets:        394/0            Tx Packets:          0/0
                                     Tx Retries:          0/0
54 Mbps:
 Rx Packets:        850/0            Tx Packets:          0/0
                                     Tx Retries:          0/0

[SU]:
M0:
 Rx Packets:      17034/0            Tx Packets:     354337/0
                                     Tx Retries:      17074/0
M1:
 Rx Packets:      29096/0            Tx Packets:      36755/0
                                     Tx Retries:     127215/0
M2:
 Rx Packets:      42855/0            Tx Packets:      82986/0
                                     Tx Retries:     231767/0
M3:
 Rx Packets:      55170/0            Tx Packets:     339425/0
                                     Tx Retries:     107398/0
M4:
 Rx Packets:      82253/0            Tx Packets:    1698006/0
                                     Tx Retries:     398620/0
M5:
 Rx Packets:     228984/0            Tx Packets:    3491802/0
                                     Tx Retries:    1265598/0
M6:
 Rx Packets:     236915/0            Tx Packets:   14587013/0
                                     Tx Retries:    3532183/0
M7:
 Rx Packets:    2283900/0            Tx Packets:   44115505/0
                                     Tx Retries:   10928276/0

[MU]:
M6:
 Rx Packets:          0/0            Tx Packets:        336/0
                                     Tx Retries:         29/0
M7:
 Rx Packets:          0/0            Tx Packets:        122/0
                                     Tx Retries:       1374/0
M8:
 Rx Packets:          0/0            Tx Packets:          1/0
                                     Tx Retries:       1754/0
M9:
 Rx Packets:          0/0            Tx Packets:          0/0
                                     Tx Retries:        640/0


Beacons missed: 0-30s 31-60s 61-90s 90s+
                     0      0      0    0

Vap TX statistics:
vap      ssid     MGMT      Beacon         DATA       [BK         BE         VI       VO] Bytes(Data) QosRetries AMDPU-SubFrameRetries
  1   CorpNet 100502/4 42723779/48 103112436/13 3958248/0 75641513/0 26490942/0 3089510/0 587050666/0 35222485/0                   0/0
  2  GuestNet  25675/0 42737305/48     472136/0  233805/0   238177/0        0/0      57/0 568646631/0   133086/0                   0/0
  3 DeviceNet  20609/0 42737320/48      31762/0     130/0    31492/0        0/0     135/0  41697610/0     5217/0                   0/0

Vap RX statistics:
vap      ssid     MGMT       DATA   [BK         BE         VI       VO]  Bytes(Data)
  1   CorpNet 945009/0 73834778/0 389/0 49220485/0 44389340/0 1734043/0 4175456208/0
  2  GuestNet   4623/0   216838/0  11/0   214868/0        0/0      18/0   30930977/0
  3 DeviceNet    468/0     6179/0 102/0     5524/0        0/0       0/0    2034118/0

字符串

脚本:

re_patterns = [
    '(Dot11Radio[0-2])\sis\s(UP|DOWN).+(UP|DOWN)$',
    '(Beacons missed:)(.+?)$\n\s+(.+?)$'
]

generic_re = re.compile( r'|'.join( re_patterns), re.MULTILINE|re.IGNORECASE )
beacons = (re.findall(generic_re, itext))

print(accesspoints['host'])
print (*beacons)

输出:

ACCESSPOINT01
('Dot11Radio0', 'UP', 'UP', '', '', '') ('', 'Beacons missed:', ' 0-30s 31-60s 61-90s 90s+', '0      0      0    0') ('Dot11Radio1', 'UP', 'UP', '', '', '') ('', 'Beacons missed:', ' 0-30s 31-60s 61-90s 90s+', '198      6      0    0')

预期输出:

ACCESSPOINT01
('Dot11Radio0', 'UP', 'UP') ('Beacons missed:', ' 0-30s 31-60s 61-90s 90s+', '0      0      0    0') ('Dot11Radio1', 'UP', 'UP') ('Beacons missed:', ' 0-30s 31-60s 61-90s 90s+', '198      6      0    0')


除此之外,我如何打印捕获组以便获得与输入文本类似的内容?

ACCESSPOINT01
    Dot11Radio0 UP/UP
        Beacons missed:
            0-30s 31-60s 61-90s 90s+
            0      0      0    0

    Dot11Radio1 UP/UP
        Beacons missed:
            0-30s 31-60s 61-90s 90s+
            198      6      0    0

disho6za

disho6za1#

当regexp包含捕获组时,re.findall()返回所有捕获组匹配的元组列表。
在regexp中有两个备选项,每个备选项都包含捕获组。如果第一个备选项匹配,则第二个备选项中的捕获组不匹配任何内容,因此这些组将得到空字符串。相反,当第二个备选项匹配时,您将获得第一个备选项中的捕获组的空字符串。
因此,根据哪个替代匹配,元组的前3个或后3个元素将是空字符串。这使您可以知道哪个选项匹配。
如果你不想这样做,就不要把所有的选项放在一个正则表达式中。分别匹配每一个,然后将两个结果合并。

beacons = []
for pat in re_patterns:
    beacons.extend(re.findall(pat, itext, flags=re.MULTILINE|re.I))

字符串
顺便说一句,你显示的输出不是我得到的。我明白

('Dot11Radio0', 'UP', 'UP', '', '', '') ('', '', '', 'Beacons missed:', ' 0-30s 31-60s 61-90s 90s+', '0      0      0    0') ('Dot11Radio1', 'UP', 'UP', '', '', '') ('', '', '', 'Beacons missed:', ' 0-30s 31-60s 61-90s 90s+', '0      0      0    0')

相关问题