如何在lis中使用python中的regex匹配数字

2024-10-01 09:40:22 发布

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

我有srt格式的字幕 我有功能

def clearSubtitles(subtitles):
        for i in subtitles:
             if re.search("^\r$", i) != None :
                  subtitles.remove(i)
             if  re.search("^\d+\r$", i) != None:
                   subtitles.remove(i)  

在列表中我有subtitles['0\r','00:59:58,084 --> 00:59:58,888\r','Come on!\r']

我需要匹配大小写0\r中的第一个短语,但是^\d+\r$匹配我timewindows(00:59:58,084 --> 00:59:58,888\r)。。有人能帮帮我吗?在


Tags: in功能renone列表forsearchif
2条回答

所以你需要用一个数字来匹配行?在

re.search(r"^\d\r", i)

好吧,我想我现在明白你想删除什么了。试试这个:

import re

cleared_subtitles = [subtitle for subtitle in subtitles if not re.match(r'\d*\r')]

这将生成一个新列表,其中包含以0或更多数字开头、以removed结尾的所有元素。重新匹配要求regexp匹配整个字符串,不像搜索. 在

相关问题 更多 >