'python regular expression - re.findall() in list

This is my list:

lista=[u'REG_S_3_UMTS_0_0 (RNC)', u'REG_S_3_UMTS_0_1 (RNC)', u'REG_S_3_UMTS_0_2 (RNC)', u'REG_S_2_GSM_NORT_CBSP_bsc_0_0 (BSC)', u'REG_S_2_GSM_NORT_CBSP_bsc_0_1 (BSC)', u'REG_S_2_GSM_NORT_CBSP_bsc_0_2 (BSC)', u'REG_S_3_GSM_ERIC_CBSP_bsc_0_0 (BSC)', u'REG_S_3_GSM_ERIC_CBSP_bsc_0_1 (BSC)', u'REG_S_3_GSM_ERIC_CBSP_bsc_0_2 (BSC)', u'REG_S_3_GSM_HUAP_CBSM_bsc_0_0 (BSC)', u'REG_S_3_GSM_HUAP_CBSM_bsc_0_1 (BSC)', u'REG_S_3_GSM_HUAP_CBSM_bsc_0_2 (BSC)', u'REG_S_3_GSM_HUA_CBSM_bsc_0_0 (BSC)', u'REG_S_3_GSM_HUA_CBSM_bsc_0_1 (BSC)', u'REG_S_3_GSM_HUA_CBSM_bsc_0_2 (BSC)', u'REG_S_3_GSM_IPAC_SABP_bsc_0_0 (BSC)', u'REG_S_3_GSM_IPAC_SABP_bsc_0_1 (BSC)', u'REG_S_3_GSM_IPAC_SABP_bsc_0_2 (BSC)', u'REG_S_3_GSM_NOKI_CLNS_bsc_0_0 (BSC)', u'REG_S_3_GSM_NOKI_CLNS_bsc_0_1 (BSC)', u'REG_S_3_GSM_NOKI_CLNS_bsc_0_2 (BSC)', u'REG_S_3_GSM_NOKI_RFC1_bsc_0_0 (BSC)', u'REG_S_3_GSM_NOKI_RFC1_bsc_0_1 (BSC)', u'REG_S_3_GSM_NOKI_RFC1_bsc_0_2 (BSC)', u'REG_S_3_GSM_NORT_CBSP_bsc_0_0 (BSC)', u'REG_S_3_GSM_NORT_CBSP_bsc_0_1 (BSC)', u'REG_S_3_GSM_NORT_CBSP_bsc_0_2 (BSC)', u'REG_S_3_GSM_SIEM_BSCI_bsc_0_0 (BSC)', u'REG_S_3_GSM_SIEM_BSCI_bsc_0_1 (BSC)', u'REG_S_3_GSM_SIEM_BSCI_bsc_0_2 (BSC)', u'REG_S_GSM_ERIC_CBSP_bsc_0_0 (BSC)', u'REG_S_GSM_ERIC_CBSP_bsc_0_1 (BSC)', u'REG_S_GSM_ERIC_CBSP_bsc_0_2 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_0 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_1 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_2 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_0 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_1 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_2 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_0 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_1 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_2 (BSC)', u'Pool ID: 200']

And that's my function:

def Filter_List(lista):

     string = ''.join(lista)
     match = re.findall(r"\(([A-Z]+)\)|Pool ID", string)
     return match

As a result I get:

[u'RNC', u'RNC', u'RNC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'BSC', u'']

But the last element (it should be: Pool ID) doesn't display.There's only: u'' . Does anyone know how should I change my expression? Thanks in advance !!!



Solution 1:[1]

Note that re.findall returns (list of) tuples if a regex pattern has capture groups defined. Remove it:

import re
lista=[u'REG_S_3_UMTS_0_0 (RNC)', u'REG_S_3_UMTS_0_1 (RNC)', u'REG_S_3_UMTS_0_2 (RNC)', u'REG_S_2_GSM_NORT_CBSP_bsc_0_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BSC)', u'REG_S_GSM_ERIC_CBSP_bsc_0_2 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_0 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_1 (BSC)', u'REG_S_GSM_HUAP_CBSM_bsc_0_2 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_0 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_1 (BSC)', u'REG_S_GSM_HUA_CBSM_bsc_0_2 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_0 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_1 (BSC)', u'REG_S_GSM_NORT_CBSP_bsc_0_2 (BSC)', u'Pool ID: 200']
string = ''.join(lista)
match = re.findall(r"\([A-Z]+\)|Pool ID", string)
print(match)

See this Python demo returning

[u'(RNC)', u'(RNC)', u'(RNC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'(BSC)', u'Pool ID']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Wiktor Stribiżew