0

我有 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)..有人可以帮助我吗?

4

2 回答 2

1

好的,所以我想我现在了解您要删除的内容。试试这个:

import re

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

这将构建一个新列表,其中包含所有以 0 或多个数字开头并以删除 \r 结尾的元素。re.match 要求正则表达式匹配整个字符串,这与 re.search 不同。

于 2013-05-10T20:19:58.240 回答
0

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

re.search(r"^\d\r", i)
于 2013-05-10T19:42:41.183 回答