如何在python中使用re函数

我有以下列表:

 ['[infotype02]', 'lastModifiedOn = serial<customMapping>', 'customString18 = BADGE_NUMBER<move>', 'firstName = FIRST_NAME<move>', 'lastName = LAST_NAME<move>', 'customString29 = USER_NAME<move>', 'email = EMAIL_ADDRESS<move>', 'documenttype = DOC_TYPE<move>', 'documentnumber = DOC_SERIA<customMapping>', 'documentnumberx2 = DOC_NUMBER<customMapping>', 'issuedate = DOC_ISSUE_DATE<move>', 'issueauthority = DOC_ISSUER<move>', 'nationalId = CNP<move>', 'company = COMPANY<move>', 'phoneNumber = PHONE_NUMBER<move>', 'startDate = HIRE_DATE<customMapping>', 'startDatex2 = TERMINATION_DATE<customMapping>', '[/infotype02]', '[infotype02]', 'lastModifiedOn = serial<customMapping>', 'customString18 = BADGE_NUMBER<move>', 'firstName = FIRST_NAME<move>', 'lastName = LAST_NAME<move>', 'customString29 = USER_NAME<move>', 'email = EMAIL_ADDRESS<move>', 'documenttype = DOC_TYPE<move>', 'documentnumber = DOC_SERIA<customMapping>', 'documentnumberx2 = DOC_NUMBER<customMapping>', 'issuedate = DOC_ISSUE_DATE<move>', 'issueauthority = DOC_ISSUER<move>', 'nationalId = CNP<move>', 'company = COMPANY<move>', 'phoneNumber = PHONE_NUMBER<move>', 'startDate = HIRE_DATE<customMapping>', 'startDatex2 = TERMINATION_DATE<customMapping>', '[/infotype02]']

对于列表中的 i;我 =[infotype02]

我尝试使用 re 表达式来获取 [] 之间的字符串,预期结果infotype02

        result = re.search('[(.*)]', i)

然后尝试将 result.group(1) 附加到新列表并返回错误:

    lst.append(result.group(1))
    AttributeError: 'NoneType' object has no attribute 'group'

我不明白我的 re 表达式有什么问题,为什么它没有找到 [] 之间的字符串


缥缈止盈
浏览 115回答 1
1回答

鸿蒙传说

您可以使用一个简单for-loop的方法来完成此操作:data = ['[infotype02]', 'lastModifiedOn = serial<customMapping>', 'customString18 = BADGE_NUMBER<move>', 'firstName = FIRST_NAME<move>',&nbsp;&nbsp; &nbsp; 'lastName = LAST_NAME<move>', 'customString29 = USER_NAME<move>', 'email = EMAIL_ADDRESS<move>', 'documenttype = DOC_TYPE<move>',&nbsp;&nbsp; &nbsp; 'documentnumber = DOC_SERIA<customMapping>', 'documentnumberx2 = DOC_NUMBER<customMapping>', 'issuedate = DOC_ISSUE_DATE<move>',&nbsp;&nbsp; &nbsp; 'issueauthority = DOC_ISSUER<move>', 'nationalId = CNP<move>', 'company = COMPANY<move>', 'phoneNumber = PHONE_NUMBER<move>',&nbsp;&nbsp; &nbsp; 'startDate = HIRE_DATE<customMapping>', 'startDatex2 = TERMINATION_DATE<customMapping>', '[/infotype02]', '[infotype02]',&nbsp;&nbsp; &nbsp; 'lastModifiedOn = serial<customMapping>', 'customString18 = BADGE_NUMBER<move>', 'firstName = FIRST_NAME<move>', 'lastName = LAST_NAME<move>',&nbsp; &nbsp; 'customString29 = USER_NAME<move>', 'email = EMAIL_ADDRESS<move>', 'documenttype = DOC_TYPE<move>', 'documentnumber = DOC_SERIA<customMapping>',&nbsp;&nbsp; &nbsp; 'documentnumberx2 = DOC_NUMBER<customMapping>', 'issuedate = DOC_ISSUE_DATE<move>', 'issueauthority = DOC_ISSUER<move>', 'nationalId = CNP<move>',&nbsp;&nbsp; &nbsp; 'company = COMPANY<move>', 'phoneNumber = PHONE_NUMBER<move>', 'startDate = HIRE_DATE<customMapping>', 'startDatex2 = TERMINATION_DATE<customMapping>',&nbsp;&nbsp; &nbsp; '[/infotype02]']new_list = []for d in data:&nbsp; &nbsp; if d[0] == '[' and not d[1] == '/':&nbsp; &nbsp; #if re.match(r"\[[^/](.*)\]", d): # If you want to use `re`&nbsp; &nbsp; &nbsp; &nbsp; new_list.append(d[1:-1])print(new_list)输出:['infotype02', 'infotype02']因为您在给定列表中有 2 个这些标签。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python