正则表达式不会产生预期的结果

时间:2019-01-16 15:49:42

标签: python regex

我使用Python 2.7 re findall查找匹配项列表,但第一种情况比我想要的要多。
我的源代码在这里,从长字符串中找到一些导入数据。

结果列表为:

 ('1', 'GLOBAL_SETTINGS')
 ('10', 'TX_MULTI_VERIFICATION 2437 DSSS-1 NON_HT BW-20 TX1') 
 ('12', 'TX_MULTI_VERIFICATION 2412 CCK-11 NON_HT BW-20 TX1')

具有项目1、10、12。这不是我想要的。我想要项目9、11、12

import re

s='''
Summary
1.GLOBAL_SETTINGS ____________________________________________________________
ANALYSIS_11AC_AMPLITUDE_TRACKING : 0 
ANALYSIS_11AC_DECODE_PSDU      : 0 

PER_WAVEFORM_PREFIX_11AG       :  
PER_WAVEFORM_PREFIX_11B        :  
PER_WAVEFORM_PREFIX_11N        :  
VSA_PORT_CHAIN_MAP             : 0,0,0,0 
VSG_PORT_CHAIN_MAP             : 0,0,0,0 
Test Time = 0.001 s
             :[Info] Function completed.

2.CONNECT_IQ_TESTER __________________________________________________________
APP_ID                         : 1 
APT_ENABLE                     : 0 
DH_ENABLE                      : 0 
 :                               
IQ_NXN_VERSION           :                               
IQ_ZIGBEE_SIGNAL_VERSION :                               
TEST_MANAGER_VERSION     :3.0.0 (2012-8-24)              
3.INSERT_DUT _________________________________________________________________
RELOAD_DUT_DLL                 : 0 
CONNECTION_STRING              : 192.168.100.1:2390 
DUT_DLL_FILENAME               : QCA.dll 
EEPROM_FILENAME                :  
OPTION_STRING                  :  
Test Time = 1.020 s
DUT_VERSION              :QLIB V6        
ERROR_MESSAGE            :[Info] Function completed.

VDUT_VERSION             :3.0.0 (2012-8-24)              
4.INITIALIZE_DUT _____________________________________________________________
Test Time = 0.308 s
DUT_DRIVER_INFO          :5.1                  
DUT_NAME                 :QCA_                       
ERROR_MESSAGE            :[Info] Function completed.

5.LOAD_PATH_LOSS_TABLE _______________________________________________________
RX_PATH_LOSS_FILE              : path_loss.csv 
TX_PATH_LOSS_FILE              : path_loss.csv 
Test Time = 0.001 s
ERROR_MESSAGE            :[Info] Function completed.

6.TX_CALIBRATION _____________________________________________________________
CABLE_LOSS_DB_1                : 0.00 dB
CABLE_LOSS_DB_2                : 0.00 dB
CABLE_LOSS_DB_3                : 0.00 dB
CABLE_LOSS_DB_4                : 0.00 dB
DUT_SETTLE_TIME                : 0.00 MS
SAMPLING_TIME_US               : 150.00 uS
TX_CAL_TIMEOUT_MS              : 600000.00 MS
Test Time = 33.392 s
CABLE_LOSS_DB_2          :          14.90              dB (, )
CAL_RESULT_1             :                               
CAL_RESULT_2             :                               
CAL_RESULT_3             :                               
CAL_RESULT_4             :                               
ERROR_MESSAGE            :[Info] Function completed.

7.TX_CLPC_CALIBRATION ________________________________________________________
CABLE_LOSS_DB_1                : 0.00 dB
CABLE_LOSS_DB_2                : 0.00 dB
CABLE_LOSS_DB_3                : 0.00 dB
CABLE_LOSS_DB_4                : 0.00 dB
SAMPLING_TIME_US               : 150.00 uS
TX_CAL_TIMEOUT_MS              : 600000.00 MS
    Skipped by ALWAYS_SKIP

8.FINALIZE_EEPROM ____________________________________________________________
COMMIT_EEPROM_TEMPLATE         : 20 
COMPRESS                       : 1 
MEMORY_SIZE                    : 0 
MEMORY_TYPE                    : 5 
OVERWRITE                      : 1 
SECTION_MASK_BIT0_ALL          : 0 
SECTION_MASK_BIT10_SDIO_PID    : 0 
S
SECTION_MASK_BIT6_5G_CONTROL_DATA : 0 
SECTION_MASK_BIT7_CONFIG_DATA  : 0 
SECTION_MASK_BIT8_CUSTOMER_DATA : 0 
SECTION_MASK_BIT9_USB_VID_PID  : 0 
    Skipped by ALWAYS_SKIP
9.TX_MULTI_VERIFICATION 2412 DSSS-1 NON_HT BW-20 TX1 _________________________
ARRAY_HANDLING_METHOD          : 0 
BSS_FREQ_MHZ_PRIMARY           : 2412 MHz
BSS_FREQ_MHZ_SECONDARY         : 0 MHz
CH_FREQ_MHZ                    : 2412 MHz
PHASE_ERR_1              :           0.37           Degree (, )
PHASE_NOISE_RMS_ALL      :           0.00           Degree (, )
POWER_AVG_1              :          21.07             dBm (, )
POWER_AVG_DBM            :          21.07             dBm (19.00, 25.00)--[failed]
SYMBOL_CLK_ERR           :          -5.76             ppm (, )
TX_POWER_DBM             :          22.00             dBm (, )
VIOLATION_PERCENT        :           0.00               % (0.00, 0.00)
SPECTRUM_FLATNESS_RAW_DATA:Not Display                  dB (, )
SPECTRUM_RAW_DATA_X      :Not Display               dBm/100kHz (, )
SPECTRUM_RAW_DATA_Y      :Not Display               dBm/100kHz (, )
ERROR_MESSAGE            :[Info] Function completed.

10.TX_MULTI_VERIFICATION 2437 DSSS-1 NON_HT BW-20 TX1 ________________________
ARRAY_HANDLING_METHOD          : 0 
BSS_FREQ_MHZ_PRIMARY           : 2437 MHz
BSS_FREQ_MHZ_SECONDARY         : 0 MHz
CH_FREQ_MHZ                    : 2437 MHz
CH_FREQ_MHZ_PRIMARY_20MHz      : 0 MHz
ENABLE_EVM                     : 1 
ENABLE_MASK                    : 1 
ENABLE_POWER                   : 0 
ENABLE_SPECTRUM                : 1 
NUM_STREAM_11AC                : 1 
TX1                            : 1 
TX2                            : 0 
TX3                            : 0 

POWER_AVG_1              :          20.83             dBm (, )
POWER_AVG_DBM            :          20.83             dBm (19.00, 25.00)
SYMBOL_CLK_ERR           :          -6.03             ppm (, )
TX_POWER_DBM             :          22.00             dBm (, )
VIOLATION_PERCENT        :           0.00               % (0.00, 0.00)
SPECTRUM_FLATNESS_RAW_DATA:Not Display                  dB (, )
SPECTRUM_RAW_DATA_X      :Not Display               dBm/100kHz (, )
SPECTRUM_RAW_DATA_Y      :Not Display               dBm/100kHz (, )
ERROR_MESSAGE            :[Info] Function completed.

11.TX_MULTI_VERIFICATION 2472 DSSS-1 NON_HT BW-20 TX1 ________________________
ARRAY_HANDLING_METHOD          : 0 
BSS_FREQ_MHZ_PRIMARY           : 2472 MHz
BSS_FREQ_MHZ_SECONDARY         : 0 MHz
PHASE_ERR_1              :           0.37           Degree (, )
PHASE_NOISE_RMS_ALL      :           0.00           Degree (, )
POWER_AVG_1              :          20.86             dBm (, )
POWER_AVG_DBM            :          20.86             dBm (19.00, 25.00)--[failed]
SYMBOL_CLK_ERR           :          -6.26             ppm (, )
TX_POWER_DBM             :          22.00             dBm (, )
VIOLATION_PERCENT        :           0.00               % (0.00, 0.00)
SPECTRUM_FLATNESS_RAW_DATA:Not Display                  dB (, )
SPECTRUM_RAW_DATA_X      :Not Display               dBm/100kHz (, )
SPECTRUM_RAW_DATA_Y      :Not Display               dBm/100kHz (, )
ERROR_MESSAGE            :[Info] Function completed.

12.TX_MULTI_VERIFICATION 2412 CCK-11 NON_HT BW-20 TX1 ________________________
ARRAY_HANDLING_METHOD          : 0 
BSS_FREQ_MHZ_PRIMARY           : 2412 MHz
BSS_FREQ_MHZ_SECONDARY         : 0 MHz
CH_FREQ_MHZ                    : 2412 MHz
CH_FREQ_MHZ_PRIMARY_20MHz      : 0 MHz
PHASE_ERR_1              :           0.97           Degree (, )
PHASE_NOISE_RMS_ALL      :           0.42           Degree (, )
POWER_AVG_1              :          21.14             dBm (, )
POWER_AVG_DBM            :          21.14             dBm (19.00, 25.00)--[failed]
SYMBOL_CLK_ERR           :          -6.06             ppm (, )
TX_POWER_DBM             :          22.00             dBm (, )
VIOLATION_PERCENT        :           0.00               % (0.00, 0.00)
SPECTRUM_FLATNESS_RAW_DATA:Not Display                  dB (, )
SPECTRUM_RAW_DATA_X      :Not Display               dBm/100kHz (, )
SPECTRUM_RAW_DATA_Y      :Not Display               dBm/100kHz (, )
ERROR_MESSAGE            :[Info] Function completed.'''





if __name__ == '__main__':



    t=re.findall(r'(\d{1,2})\.(.{50,100}_{15}).+?\[failed\]',s,re.M|re.DOTALL)   
    print t

2 个答案:

答案 0 :(得分:2)

更新

如果您坚持使用正则表达式来跟踪失败,则可以执行以下操作:

for i in s.split('\n'):
    number = re.findall(r'^\d+', i)
    if number: failure = re.findall(r'^(\d+)\.(.*?)\s_{2,}', i)
    if 'failed' in i:
        print(failure)

返回:

[('9', 'TX_MULTI_VERIFICATION 2412 DSSS-1 NON_HT BW-20 TX1')]
[('11', 'TX_MULTI_VERIFICATION 2472 DSSS-1 NON_HT BW-20 TX1')]
[('12', 'TX_MULTI_VERIFICATION 2412 CCK-11 NON_HT BW-20 TX1')]

您可以在操作here中看到此正则表达式的形式。

答案 1 :(得分:0)

您可以使用re.split来分成几个部分,然后在每个部分中寻找[failed]字符串:

splitted = re.split(r'(\d{1,2})\.(.*)(?= _{3,})', text)

failed = [(splitted[i-2], splitted[i-1]) for i, s in enumerate(splitted) if re.search(r'\[failed\]', s)]

failed
# [('9', 'TX_MULTI_VERIFICATION 2412 DSSS-1 NON_HT BW-20 TX1'), 
#  ('11', 'TX_MULTI_VERIFICATION 2472 DSSS-1 NON_HT BW-20 TX1'), 
#  ('12', 'TX_MULTI_VERIFICATION 2412 CCK-11 NON_HT BW-20 TX1')]

此处re.split将文本分为三个子组:
 1.第1组(节号)
 2.第2组(部分标题)
 3.第3组(节体)

如果第3组中存在匹配项,则请参考第1组和第2组-因此,splitted[i-2]splitted[i-1]