簡體   English   中英

如何比較兩個正則表達式匹配?

[英]How to compare two regex matches?

我正在為電視連續劇制作軟件,我想驗證兩個正則表達式匹配在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
    }
}

我怎么做?

將文件名轉換為一種格式,並將其保留在集合中以與它們匹配:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM