Jakub Baski Gabčo
Jakub Baski Gabčo

Reputation: 355

How to match numbers using regex in python in list

I have subtitles in srt format I have function

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)  

in list i have subtitles['0\r','00:59:58,084 --> 00:59:58,888\r','Come on!\r']

i need to match the first number of phrase in the case 0\r but the ^\d+\r$ matches me timewindows(00:59:58,084 --> 00:59:58,888\r) .. Can somebody help me ?

Upvotes: 0

Views: 188

Answers (2)

Peter DeGlopper
Peter DeGlopper

Reputation: 37319

OK, so I think I now understand what you're trying to remove. Try this:

import re

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

This builds a new list with all elements that begin with 0 or more digits and end with \r removed. re.match requires that the regexp match the entire string, unlike re.search.

Upvotes: 1

wroniasty
wroniasty

Reputation: 8052

So you need to match lines with a single digit ?

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

Upvotes: 0

Related Questions