Regular expression to match season and episode

I am making a small application for myself and I want to find strings that match the pattern, but I could not find the correct regular expression.

Stargate.SG-1.S01E08.iNT.DVDRip.XviD-LOCK.avi 

This is the decryption of the string that I have, and I only want to know if it contains the substring S [NUMBER] E [NUMBER] with each number no more than two digits.

Can you give me a hint?

+4
source share
4 answers

Regex

Here is a regular expression using the named groups:

 S(?<season>\d{1,2})E(?<episode>\d{1,2}) 

Using

Then you can get the named groups (season and episode) as follows:

 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!"); } 

Regular expression explanation

 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 
+9
source

There's a great online testing site here: http://gskinner.com/RegExr/

Using this, here you need a regular expression:

 S\d\dE\d\d 

You can do a lot of fancy tricks besides this!

+1
source

Take a look at some of the media programs, such as XBMC, all have pretty reliable regular filters for TV shows.

Look here , here

0
source

The regular expression that I would put for S [NUMBER1] E [NUMBER2] was

 S(\d\d?)E(\d\d?) // (\d\d?) means one or two digit 

You can get NUMBER1 at <matchresult>.group(1) , NUMBER2 at <matchresult>.group(2) .

0
source

All Articles