正则表达式问题解析季节/剧集模式

时间:2016-08-14 09:21:06

标签: c# regex

为什么这个正则表达式模式没有正确解析字符串“Season 02 Episode 01”?

例如,这不匹配:

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);  

2 个答案:

答案 0 :(得分:1)

使用*代替?

?是0或1次。 *为0次或更多次。

答案 1 :(得分:1)

  

以“S”开头,可以包含更多字母[...]

您的意思是+,而不是?

var pattern = @"S\w+\s+(\d+)\s+E\w+\s+(\d+)";

请注意,此正则表达式非常不明确。注意误报。我建议使表达更具体。