Xavier Peña Xavier Peña - 3 months ago 11
C# Question

Regex issue parsing Season / Episode pattern

Why doesn't this regex pattern parse the string "Season 02 Episode 01" properly?

For example, this is not a match:

var fileName = "Its Always Sunny in Philadelphia Season 02 Episode 01 - Charlie Gets Crippled.avi"

// Regex explanation:
// Starts with "S" and can contain more letters, can continue with space, then contains two numbers.
// Then starts with "E" again and can contain more letters, can continue with space, then contains two numbers.
var pattern = @"S\w?\s?(\d\d)\s?E\w?\s?(\d\d)";
var regex = new Regex(pattern, RegexOptions.IgnoreCase);
var match = regex.Match(fileName);

Answer

Use * instead of ?

? is for 0 or 1 time. * is for 0 or more times.

Comments