Prixeler Prixeler - 3 months ago 25
Python Question

Python Regex stop on string

So I want to search using regex for seasons which are not followed by episode number and I have the following list :

string = ['Fear the walking dead Season 2 Episode 9',
'Veep Season 5',
'Martine Season 2 (unknown number of episodes)',
'New Girl Season 5 Episode 16']


I've written this code
re.search('.+? Season [0-9]{1,2}', string, re.I)
but it seems to take into consideration the series with an episode number also.
I want it to return True only on
Veep Season 5

Answer

I would recommend using ^ and $ to match from the beginning of a line to the end. So you can change your regex to:

re.search('^(.+?Season\s[0-9]{1,2})$', string, re.I | re.M)