From e5632019db9a630a80d4b62c82f060322e172695 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sat, 9 Feb 2019 20:46:56 +0100 Subject: [PATCH] Simplified more RegexReplace instances. --- src/NzbDrone.Core/Parser/Parser.cs | 45 +++++++++++++++--------- src/NzbDrone.Core/Parser/RegexReplace.cs | 21 ++++++++--- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/NzbDrone.Core/Parser/Parser.cs b/src/NzbDrone.Core/Parser/Parser.cs index 3de56bc93..7fac3c646 100644 --- a/src/NzbDrone.Core/Parser/Parser.cs +++ b/src/NzbDrone.Core/Parser/Parser.cs @@ -301,26 +301,31 @@ namespace NzbDrone.Core.Parser //Regex to detect whether the title was reversed. private static readonly Regex ReversedTitleRegex = new Regex(@"(?:^|[-._ ])(p027|p0801|\d{2,3}E\d{2}S)[-._ ]", RegexOptions.Compiled); - private static readonly Regex NormalizeRegex = new Regex(@"((?:\b|_)(??*:|]|848x480|1280x720|1920x1080|3840x2160|4096x2160|(8|10)b(it)?|10-bit)\s*?", + private static readonly RegexReplace SimpleTitleRegex = new RegexReplace(@"(?:(480|720|1080|2160)[ip]|[xh][\W_]?26[45]|DD\W?5\W1|[<>?*:|]|848x480|1280x720|1920x1080|3840x2160|4096x2160|(8|10)b(it)?|10-bit)\s*?", + string.Empty, RegexOptions.IgnoreCase | RegexOptions.Compiled); - private static readonly Regex WebsitePrefixRegex = new Regex(@"^\[\s*[a-z]+(\.[a-z]+)+\s*\][- ]*|^www\.[a-z]+\.(?:com|net)[ -]*", - RegexOptions.IgnoreCase | RegexOptions.Compiled); + private static readonly RegexReplace WebsitePrefixRegex = new RegexReplace(@"^\[\s*[a-z]+(\.[a-z]+)+\s*\][- ]*|^www\.[a-z]+\.(?:com|net)[ -]*", + string.Empty, + RegexOptions.IgnoreCase | RegexOptions.Compiled); private static readonly Regex SixDigitAirDateRegex = new Regex(@"(?<=[_.-])(?(?[1-9]\d{1})(?[0-1][0-9])(?[0-3][0-9]))(?=[_.-])", RegexOptions.IgnoreCase | RegexOptions.Compiled); - private static readonly Regex CleanReleaseGroupRegex = new Regex(@"^(.*?[-._ ](S\d+E\d+)[-._ ])|(-(RP|1|NZBGeek|Obfuscated|Scrambled|sample|Pre|postbot|xpost))+$", + private static readonly RegexReplace CleanReleaseGroupRegex = new RegexReplace(@"^(.*?[-._ ](S\d+E\d+)[-._ ])|(-(RP|1|NZBGeek|Obfuscated|Scrambled|sample|Pre|postbot|xpost))+$", + string.Empty, RegexOptions.IgnoreCase | RegexOptions.Compiled); - private static readonly Regex CleanTorrentSuffixRegex = new Regex(@"\[(?:ettv|rartv|rarbg|cttv)\]$", - RegexOptions.IgnoreCase | RegexOptions.Compiled); + private static readonly RegexReplace CleanTorrentSuffixRegex = new RegexReplace(@"\[(?:ettv|rartv|rarbg|cttv)\]$", + string.Empty, + RegexOptions.IgnoreCase | RegexOptions.Compiled); private static readonly Regex CleanQualityBracketsRegex = new Regex(@"\[[a-z0-9 ._-]+\]$", RegexOptions.IgnoreCase | RegexOptions.Compiled); @@ -392,17 +397,20 @@ namespace NzbDrone.Core.Parser var releaseTitle = RemoveFileExtension(title); - var simpleTitle = SimpleTitleRegex.Replace(releaseTitle, string.Empty); - foreach (var replace in PreSubstitutionRegex) { - simpleTitle = replace.Replace(simpleTitle); + if (replace.TryReplace(ref releaseTitle)) + { + Logger.Debug("Substituted with " + releaseTitle); + } } - // TODO: Quick fix stripping [url] - prefixes. - simpleTitle = WebsitePrefixRegex.Replace(simpleTitle, string.Empty); + var simpleTitle = SimpleTitleRegex.Replace(releaseTitle); - simpleTitle = CleanTorrentSuffixRegex.Replace(simpleTitle, string.Empty); + // TODO: Quick fix stripping [url] - prefixes. + simpleTitle = WebsitePrefixRegex.Replace(simpleTitle); + + simpleTitle = CleanTorrentSuffixRegex.Replace(simpleTitle); simpleTitle = CleanQualityBracketsRegex.Replace(simpleTitle, m => { @@ -513,7 +521,7 @@ namespace NzbDrone.Core.Parser if (long.TryParse(title, out number)) return title; - return NormalizeRegex.Replace(title, string.Empty).ToLower().RemoveAccent(); + return NormalizeRegex.Replace(title).ToLower().RemoveAccent(); } public static string NormalizeEpisodeTitle(string title) @@ -552,7 +560,12 @@ namespace NzbDrone.Core.Parser { title = title.Trim(); title = RemoveFileExtension(title); - title = WebsitePrefixRegex.Replace(title, ""); + foreach (var replace in PreSubstitutionRegex) + { + if (replace.TryReplace(ref title)) + break; + } + title = WebsitePrefixRegex.Replace(title); var animeMatch = AnimeReleaseGroupRegex.Match(title); @@ -561,7 +574,7 @@ namespace NzbDrone.Core.Parser return animeMatch.Groups["subgroup"].Value; } - title = CleanReleaseGroupRegex.Replace(title, ""); + title = CleanReleaseGroupRegex.Replace(title); var matches = ReleaseGroupRegex.Matches(title); diff --git a/src/NzbDrone.Core/Parser/RegexReplace.cs b/src/NzbDrone.Core/Parser/RegexReplace.cs index f4a10db23..0dc1bd0f6 100644 --- a/src/NzbDrone.Core/Parser/RegexReplace.cs +++ b/src/NzbDrone.Core/Parser/RegexReplace.cs @@ -10,23 +10,36 @@ namespace NzbDrone.Core.Parser public class RegexReplace { private readonly Regex _regex; - private readonly string _replacement; + private readonly string _replacementFormat; + private readonly MatchEvaluator _replacementFunc; public RegexReplace(string pattern, string replacement, RegexOptions regexOptions) { _regex = new Regex(pattern, regexOptions); - _replacement = replacement; + _replacementFormat = replacement; + } + + public RegexReplace(string pattern, MatchEvaluator replacement, RegexOptions regexOptions) + { + _regex = new Regex(pattern, regexOptions); + _replacementFunc = replacement; } public string Replace(string input) { - return _regex.Replace(input, _replacement); + if (_replacementFunc != null) + return _regex.Replace(input, _replacementFunc); + else + return _regex.Replace(input, _replacementFormat); } public bool TryReplace(ref string input) { var result = _regex.IsMatch(input); - input = _regex.Replace(input, _replacement); + if (_replacementFunc != null) + input = _regex.Replace(input, _replacementFunc); + else + input = _regex.Replace(input, _replacementFormat); return result; } }