Reputation: 467
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
Upvotes: 1
Views: 772
Reputation: 76
From previous experience, I'd suggest not solely doing this with regex, but I've quickly thrown together the following snippet (after which no_episode_string will contain all of the ones without episodes).
For each season we match against ".?[0-9](.*)", which simply grabs everything up to and including the first time we encounter a number, and then take the rest of the string, which will either be empty (if there is no episode number), or non-empty if there is an episode number.
So we just check whether it is empty or not, and if it is, then we add the whole thing to no_episode_string.
import re
string = ['Fear the walking dead Season 2 Episode 9',
'Veep Season 5',
'Martine Season 2 (unknown number of episodes)',
'New Girl Seasoon 5 Episode 16']
no_episode_string = []
for season in string:
m = re.search('.*?[0-9]+(.*)', season)
if m.group(1) == "":
no_episode_string.append(m.group(0))
Upvotes: 2
Reputation: 10746
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)
Upvotes: 3