Skip to content

Commit

Permalink
feat: improve key extraction and regex handling
Browse files Browse the repository at this point in the history
- Add PatchKey method to normalize numeric keys
- Update Matcher to use PatchKey for extracted keys
- Modify ManualRuleViewModel to handle wildcard patterns correctly
  • Loading branch information
qwqcode committed Sep 23, 2024
1 parent 965feec commit 8512a2e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
4 changes: 0 additions & 4 deletions SubRenamer/Matcher/Diff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ public static string ExtractMatchKeyByDiff(DiffResult? diff, string filename)

var key = match.Groups[1].Value.Trim();

// check is pure number
if (key.All(char.IsDigit))
key = int.Parse(key).ToString(); // '01' -> '1'

return key;
}
}
11 changes: 10 additions & 1 deletion SubRenamer/Matcher/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ public static string ExtractMatchKeyRegex(string pattern, string filename)
}
return "";
}


public static string PatchKey(string key)
{
// check is pure number
if (!string.IsNullOrWhiteSpace(key) && key.All(char.IsDigit))
key = int.Parse(key).ToString(); // '01' -> '1'

return key;
}

/// <summary>
/// Merges items with the same non-empty keys by grouping them.
/// Assuming the mapping between the video and the subtitles is one-to-many (1 to N).
Expand Down
4 changes: 2 additions & 2 deletions SubRenamer/Matcher/Matcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ private static Dictionary<string, string> CalculateFileKeys(IReadOnlyList<string
// Extract Match keys
foreach (var f in files)
{
result[f] = Diff.ExtractMatchKeyByDiff(diff, Path.GetFileNameWithoutExtension(f));
result[f] = Helper.PatchKey(Diff.ExtractMatchKeyByDiff(diff, Path.GetFileNameWithoutExtension(f)));
}
}
else
{
// 2. Regex Algorithm
foreach (var f in files)
{
result[f] = Helper.ExtractMatchKeyRegex(regexPattern, Path.GetFileName(f));
result[f] = Helper.PatchKey(Helper.ExtractMatchKeyRegex(regexPattern, Path.GetFileName(f)));
}
}

Expand Down
2 changes: 1 addition & 1 deletion SubRenamer/ViewModels/ManualRuleViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private string GenerateRegex(string input)
{
var pattern = Regex.Escape(input)
.Replace(@"\$\$", @"(.+?)")
.Replace(@"\*", @"(.*?)");
.Replace(@"\*", @".*?");
return pattern;
}

Expand Down

0 comments on commit 8512a2e

Please sign in to comment.