在這種情況下,我可能會使用Regex.replace的overload,需要一個MatchEvaluator。這允許你傳遞一個帶有匹配表達式並返回替換字符串的函數。
下面是一個使用該示例的示例,它將匹配您的所有示例。我還將你的字符串嵌入到文件名中,以顯示它們在文件名中的替換方式(這似乎是你的目標)。
我在這裏爲MatchEvaluator使用了一個lambda表達式。如果你想更復雜的邏輯,你可以use a method on your class
我用了兩個正則表達式:一個唯一的號碼匹配的情況下,一個匹配一切。我經常發現使用多個簡單的正則表達式比試圖使用一個複雜的正則表達式更易於維護。
編輯:更新爲使用正則表達式的優先級列表嘗試。在列表中找到第一個匹配後,它將停止檢查
您必須確定要使用什麼規則(正則表達式)以適合您的數據的順序使用。
string[] filenames = {
"1000 Ways to Die S01E01 Life Will Kill You",
"somefile1x01description.ext",
"sometext01x01description.ext",
"sometext101description.ext",
"sometextS01Edescription.ext",
"sometextS01 Edescription.ext",
"sometextS1Edescription.ext",
"sometextS1 Edescription.ext",
"sometextS1xdescription.ext",
"24 S01xE01 12 AM"
};
string [] res = {
@"[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})", // Handles the cases where you have a delimiter and a digit on both sides, optional S
@"[sS](?<season>\d{1,2})[ xXeE]+(?<episode>\d{0,2})", // Handles the cases where you have a delimiter, a required S, but optional episode number
@"(?<season>\d{1,2})(?<episode>\d{2})" // Handles the case where you just have a 3 or 4 digit number
};
MatchEvaluator reFunc = match => // Given a Regex Match object
// An expression that returns the replacement string
"S" + // Start with the S
match.Groups["season"].Value // get the season group
.PadLeft(2,'0') + // zero pad it
"xE" + // Add the E
(match.Groups["episode"].Value.Length > 0 ? // Is there an episode number?
match.Groups["episode"].Value.PadLeft(2,'0') : // If so, zero pad it
"01" // Otherwise assume episode 01
); // End replacement expression
foreach(string name in filenames)
{
Console.WriteLine("Orig: {0}",name);
string replaced = name;
foreach (string re in res)
{
Console.WriteLine("Trying:" + re);
if(Regex.IsMatch(name,re))
{
Console.WriteLine("Matched");
replaced = Regex.Replace(name,re,reFunc);
break;
}
}
Console.WriteLine("Replaced: {0}\n\n",replaced);
}
輸出:
Orig: 1000 Ways to Die S01E01 Life Will Kill You
Trying:[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})
Matched
Replaced: 1000 Ways to Die S01xE01 Life Will Kill You
Orig: somefile1x01description.ext
Trying:[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})
Matched
Replaced: somefileS01xE01description.ext
Orig: sometext01x01description.ext
Trying:[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})
Matched
Replaced: sometextS01xE01description.ext
Orig: sometext101description.ext
Trying:[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})
Trying:[sS](?<season>\d{1,2})[ xXeE]+(?<episode>\d{0,2})
Trying:(?<season>\d{1,2})(?<episode>\d{2})
Matched
Replaced: sometextS01xE01description.ext
Orig: sometextS01Edescription.ext
Trying:[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})
Trying:[sS](?<season>\d{1,2})[ xXeE]+(?<episode>\d{0,2})
Matched
Replaced: sometextS01xE01description.ext
Orig: sometextS01 Edescription.ext
Trying:[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})
Trying:[sS](?<season>\d{1,2})[ xXeE]+(?<episode>\d{0,2})
Matched
Replaced: sometextS01xE01description.ext
Orig: sometextS1Edescription.ext
Trying:[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})
Trying:[sS](?<season>\d{1,2})[ xXeE]+(?<episode>\d{0,2})
Matched
Replaced: sometextS01xE01description.ext
Orig: sometextS1 Edescription.ext
Trying:[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})
Trying:[sS](?<season>\d{1,2})[ xXeE]+(?<episode>\d{0,2})
Matched
Replaced: sometextS01xE01description.ext
Orig: sometextS1xdescription.ext
Trying:[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})
Trying:[sS](?<season>\d{1,2})[ xXeE]+(?<episode>\d{0,2})
Matched
Replaced: sometextS01xE01description.ext
Orig: 24 S01xE01 12 AM
Trying:[sS]?(?<season>\d{1,2})[ xXeE]+(?<episode>\d{1,2})
Matched
Replaced: 24 S01xE01 12 AM
你谷歌?關閉我的頭頂部,它是'\ 1'或'\ k' –
我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –
http://www.regular-expressions.info/named.html –