我正在制作电视剧的软件,我想验证两个正则表达式匹配在foreach循环中具有相同的含义(例如,S03E01 == 03x01)。
这是我的代码:
Regex regex = new Regex(@"S?\d{1,2}[x|e]?\d{1,2}", RegexOptions.IgnoreCase);
foreach (string file in path) {
if (regex.IsMatch(file)) {
//something
}
}
我该怎么做?
答案 0 :(得分:1)
将文件名转换为一种格式并将它们保存在一个集合中以匹配它们:
Dictionary<string, string> dict = new Dictionary<string,string>();
Regex regex = new Regex(@"S?(\d{1,2})[x|e]?(\d{1,2})", RegexOptions.IgnoreCase);
foreach (string file in path)
{
var match = regex.Match(file);
if (match.Success)
{
string key = "S" + match.Groups[1].Value.PadLeft(2, '0') + "E" + match.Groups[2].Value.PadLeft(2, '0');
if (dict.ContainsKey(key))
{
// .. already in there
}
else
{
dict[key] = file;
}
}
}