如何在Python中获取regex中list的所有元素

2024-05-16 10:39:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过regex打印列表元素这是我的代码:

myresult_tv = [ 'Extinct A Horizon Guide to Dinosaurs WEB h264-WEBTUBE', 'High Noon 2019 04 05 720p HDTV DD5 1 MPEG2-NTb', 'Wyatt Cenacs Problem Areas S02E01 1080p WEBRip x264-eSc', 'Bondi Vet S05E15 720p WEB x264-GIMINI', 'If Loving You Is Wrong S04E03 Randals Stage HDTV x264-CRiMSON', 'Wyatt Cenacs Problem Areas S02E01 WEBRip x264-eSc', 'Bondi Vet S05E15 1080p WEB x264-GIMINI']


li = []

for a in myresult_tv:
    w = re.match(".*\d ", a)
    c =w.group()
    li.append(c)

print(li)

结果是:

    Traceback (most recent call last):
  File "azazzazazaaz.py", line 31, in <module>
    c =w.group()
AttributeError: 'NoneType' object has no attribute 'group'

***Repl Closed***

Tags: webgrouplitvproblemx264hdtvwyatt
3条回答

因为我不明白您期望的输出是什么,所以我使用与您相同的正则表达式。尝试使用以下代码:

li = []
for a in myresult_tv:
    try:                             # I use try... except... in case the regex doesn't work at some list elements
        w = re.search("(.*\d )", a)  # I use search instead of match
        c = w.group()
        li.append(c)
    except:
        pass

print(li)

你说过你想要每个字符串中的单个单词,你可以调用它们上的split(),这将根据空格将较大的字符串分开。你知道吗

myresult_tv = [ 'Extinct A Horizon Guide to Dinosaurs WEB h264-WEBTUBE', 'High Noon 2019 04 05 720p HDTV DD5 1 MPEG2-NTb', 'Wyatt Cenacs Problem Areas S02E01 1080p WEBRip x264-eSc', 'Bondi Vet S05E15 720p WEB x264-GIMINI', 'If Loving You Is Wrong S04E03 Randals Stage HDTV x264-CRiMSON', 'Wyatt Cenacs Problem Areas S02E01 WEBRip x264-eSc', 'Bondi Vet S05E15 1080p WEB x264-GIMINI']

li = []

for a in myresult_tv:
    for c in a.split(): 
         li.append(c)

print(li)

提供:

['Extinct', 'A', 'Horizon', 'Guide', 'to', 'Dinosaurs', 'WEB', 'h264-WEBTUBE', 'High', 'Noon', '2019', '04', '05', '720p', 'HDTV', 'DD5', '1', 'MPEG2-NTb', 'Wyatt', 'Cenacs', 'Problem', 'Areas', 'S02E01', '1080p', 'WEBRip', 'x264-eSc', 'Bondi', 'Vet', 'S05E15', '720p', 'WEB', 'x264-GIMINI', 'If', 'Loving', 'You', 'Is', 'Wrong', 'S04E03', 'Randals', 'Stage', 'HDTV', 'x264-CRiMSON', 'Wyatt', 'Cenacs', 'Problem', 'Areas', 'S02E01', 'WEBRip', 'x264-eSc', 'Bondi', 'Vet', 'S05E15', '1080p', 'WEB', 'x264-GIMINI']

如果您真的想要一个regex,re.split('\s+', s)就可以了,可以拆分任意数量的空白。你知道吗

您没有检查正则表达式是否与列表中的元素匹配。你应该这样做:

match = re.search(pattern, string)
if match:
    process(match)

相关问题 更多 >