python正则表达式 - 列表中的re.findall()

时间:2016-08-12 09:56:47

标签: regex python-2.7

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.mindefy.kidster.activity.NotificationDescriptionActivity"
    tools:showIn="@layout/activity_notification_description">
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/notificationRecyclerViewParent"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="80dp" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="serif"
        android:id="@+id/sendMessageEdittext"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
   />
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignTop="@+id/sendMessageEdittext"
        android:layout_alignParentRight="true"
        android:id="@+id/sendMessageReplyButton"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>

这就是我的功能:

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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G_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']

结果我得到了:

def Filter_List(lista):

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

但是最后一个元素(它应该是:池ID)不显示。只有:你''。有谁知道我应该如何改变我的表情?在此先感谢!!!

1 个答案:

答案 0 :(得分:3)

请注意,如果正则表达式模式定义了捕获组,re.findall将返回(列表)元组。删除它:

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_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']
string = ''.join(lista)
match = re.findall(r"\([A-Z]+\)|Pool ID", string)
print(match)

请参阅this Python demo返回

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