Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In this case, I'd probably use the <a href="http://msdn.microsoft.com/en-us/library/cft8645c.aspx" rel="nofollow">overload</a> of Regex.replace that takes a MatchEvaluator. This allows you to pass a function that takes a Match expression and returns the replacement string.</p> <p>Here's an example that uses that and will match all of your examples. I've also embedded your strings inside of filenames to show how they are replaced inside of the filenames (which seems your goal).</p> <p>I've used a lambda expression here for the MatchEvaluator. If you wanted more complex logic, you can <a href="http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.matchevaluator.aspx" rel="nofollow">use a method on your class</a></p> <p>I used two regular expressions: one to match the only numbers case and one to match everything else. I often find that using multiple simple regular expressions is far more maintainable than trying to use one complex one.</p> <p>EDIT: updated to use a priority list of regular expressions to try. It will stop checking after the first match found in the list</p> <p>You'll have to determine what rules (regexes) you want to use in what order to fit your data.</p> <pre><code>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]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2})", // Handles the cases where you have a delimiter and a digit on both sides, optional S @"[sS](?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{0,2})", // Handles the cases where you have a delimiter, a required S, but optional episode number @"(?&lt;season&gt;\d{1,2})(?&lt;episode&gt;\d{2})" // Handles the case where you just have a 3 or 4 digit number }; MatchEvaluator reFunc = match =&gt; // 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 &gt; 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); } </code></pre> <p>Output:</p> <pre><code>Orig: 1000 Ways to Die S01E01 Life Will Kill You Trying:[sS]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2}) Matched Replaced: 1000 Ways to Die S01xE01 Life Will Kill You Orig: somefile1x01description.ext Trying:[sS]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2}) Matched Replaced: somefileS01xE01description.ext Orig: sometext01x01description.ext Trying:[sS]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2}) Matched Replaced: sometextS01xE01description.ext Orig: sometext101description.ext Trying:[sS]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2}) Trying:[sS](?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{0,2}) Trying:(?&lt;season&gt;\d{1,2})(?&lt;episode&gt;\d{2}) Matched Replaced: sometextS01xE01description.ext Orig: sometextS01Edescription.ext Trying:[sS]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2}) Trying:[sS](?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{0,2}) Matched Replaced: sometextS01xE01description.ext Orig: sometextS01 Edescription.ext Trying:[sS]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2}) Trying:[sS](?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{0,2}) Matched Replaced: sometextS01xE01description.ext Orig: sometextS1Edescription.ext Trying:[sS]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2}) Trying:[sS](?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{0,2}) Matched Replaced: sometextS01xE01description.ext Orig: sometextS1 Edescription.ext Trying:[sS]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2}) Trying:[sS](?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{0,2}) Matched Replaced: sometextS01xE01description.ext Orig: sometextS1xdescription.ext Trying:[sS]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2}) Trying:[sS](?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{0,2}) Matched Replaced: sometextS01xE01description.ext Orig: 24 S01xE01 12 AM Trying:[sS]?(?&lt;season&gt;\d{1,2})[ xXeE]+(?&lt;episode&gt;\d{1,2}) Matched Replaced: 24 S01xE01 12 AM </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload