[ACCEPTED]-Regex for matching season and episode-regex
Regex
Here is the regex using named groups:
S(?<season>\d{1,2})E(?<episode>\d{1,2})
Usage
Then, you 1 can get named groups (season and episode) like this:
string sample = "Stargate.SG-1.S01E08.iNT.DVDRip.XviD-LOCK.avi";
Regex regex = new Regex(@"S(?<season>\d{1,2})E(?<episode>\d{1,2})");
Match match = regex.Match(sample);
if (match.Success)
{
string season = match.Groups["season"].Value;
string episode = match.Groups["episode"].Value;
Console.WriteLine("Season: " + season + ", Episode: " + episode);
}
else
{
Console.WriteLine("No match!");
}
Explanation of the regex
S // match 'S'
( // start of a capture group
?<season> // name of the capture group: season
\d{1,2} // match 1 to 2 digits
) // end of the capture group
E // match 'E'
( // start of a capture group
?<episode> // name of the capture group: episode
\d{1,2} // match 1 to 2 digits
) // end of the capture group
There's a great online test site here: http://gskinner.com/RegExr/
Using 2 that, here's the regex you'd want:
S\d\dE\d\d
You can 1 do lots of fancy tricks beyond that though!
Take a look at some of the media software 2 like XBMC they all have pretty robust regex 1 filters for tv shows
The regex I would put for S[NUMBER1]E[NUMBER2] is 1
S(\d\d?)E(\d\d?) // (\d\d?) means one or two digit
You can get NUMBER1 by <matchresult>.group(1)
, NUMBER2 by <matchresult>.group(2)
.
I would like to propose a little more complex 13 regex. I don't have ". : - _" because 12 i replace them with space
str_replace(
array('.', ':', '-', '_', '(', ')'), ' ',
This is the capture 11 regex that splits title to title season 10 and episode
(.*)\s(?:s?|se)(\d+)\s?(?:e|x|ep)\s?(\d+)
e.g. Da Vinci's Demons se02ep04 9 and variants https://regex101.com/r/UKWzLr/3
The only case that i can't 8 cover is to have interval between season 7 and the number, because the letter s or 6 se is becoming part if the title that does 5 not work for me. Anyhow i haven't seen such 4 a case, but still it is an issue.
Edit: I 3 managed to get around it with a second line
$title = $matches[1];
$title = preg_replace('/(\ss|\sse)$/i', '', $title);
This 2 way i remove endings on ' s' and ' se' if 1 name is part of series
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.