From d9744c41022e0b6ae8e18013e0268cc701ee8fc2 Mon Sep 17 00:00:00 2001 From: Qstick Date: Tue, 10 Sep 2019 20:54:21 -0400 Subject: [PATCH] Changed: Simplified more RegexReplace instances Co-Authored-By: taloth --- src/NzbDrone.Core/Parser/Parser.cs | 45 ++++++++++++----------- src/NzbDrone.Core/Parser/RegexReplace.cs | 46 ++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 20 deletions(-) create mode 100644 src/NzbDrone.Core/Parser/RegexReplace.cs diff --git a/src/NzbDrone.Core/Parser/Parser.cs b/src/NzbDrone.Core/Parser/Parser.cs index 307f5860d..92716ff67 100644 --- a/src/NzbDrone.Core/Parser/Parser.cs +++ b/src/NzbDrone.Core/Parser/Parser.cs @@ -155,30 +155,35 @@ namespace NzbDrone.Core.Parser new Regex(@"^b00bs$", RegexOptions.Compiled | RegexOptions.IgnoreCase) }; - private static readonly Regex NormalizeRegex = new Regex(@"((?:\b|_)(?*:|]|848x480|1280x720|1920x1080|3840x2160|4096x2160|(8|10)b(it)?)\s*", + private static readonly RegexReplace SimpleTitleRegex = new RegexReplace(@"(?:(480|720|1080|2160|320)[ip]|[xh][\W_]?26[45]|DD\W?5\W1|[<>*:|]|848x480|1280x720|1920x1080|3840x2160|4096x2160|(8|10)b(it)?)\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 AirDateRegex = new Regex(@"^(.*?)(?\d{4})[_.-](?[0-1][0-9])[_.-](?[0-3][0-9])|(?[0-1][0-9])[_.-](?[0-3][0-9])[_.-](?\d{4}))(?!\d)", 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); + RegexOptions.IgnoreCase | RegexOptions.Compiled); - private static readonly Regex CleanReleaseGroupRegex = new Regex(@"^(.*?[-._ ])|(-(RP|1|NZBGeek|Obfuscated|Scrambled|sample|Pre|postbot|xpost))+$", + private static readonly RegexReplace CleanReleaseGroupRegex = new RegexReplace(@"^(.*?[-._ ])|(-(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 ReleaseGroupRegex = new Regex(@"-(?[a-z0-9]+)(? Regex.Escape(s.Title.RemoveAccent())).ToList()).Replace(@"\ ", @"[\W_]"); @@ -385,12 +390,12 @@ namespace NzbDrone.Core.Parser var releaseTitle = RemoveFileExtension(title); - var simpleTitle = SimpleTitleRegex.Replace(releaseTitle, string.Empty); + var simpleTitle = SimpleTitleRegex.Replace(releaseTitle); // TODO: Quick fix stripping [url] - prefixes. - simpleTitle = WebsitePrefixRegex.Replace(simpleTitle, string.Empty); + simpleTitle = WebsitePrefixRegex.Replace(simpleTitle); - simpleTitle = CleanTorrentSuffixRegex.Replace(simpleTitle, string.Empty); + simpleTitle = CleanTorrentSuffixRegex.Replace(simpleTitle); var airDateMatch = AirDateRegex.Match(simpleTitle); if (airDateMatch.Success) @@ -474,7 +479,7 @@ namespace NzbDrone.Core.Parser if (long.TryParse(name, out number)) return name; - return NormalizeRegex.Replace(name, string.Empty).ToLower().RemoveAccent(); + return NormalizeRegex.Replace(name).ToLower().RemoveAccent(); } public static string NormalizeTrackTitle(this string title) @@ -500,7 +505,7 @@ namespace NzbDrone.Core.Parser { title = title.Trim(); title = RemoveFileExtension(title); - title = WebsitePrefixRegex.Replace(title, ""); + title = WebsitePrefixRegex.Replace(title); var animeMatch = AnimeReleaseGroupRegex.Match(title); @@ -509,7 +514,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 new file mode 100644 index 000000000..11fdf579f --- /dev/null +++ b/src/NzbDrone.Core/Parser/RegexReplace.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace NzbDrone.Core.Parser +{ + public class RegexReplace + { + private readonly Regex _regex; + private readonly string _replacementFormat; + private readonly MatchEvaluator _replacementFunc; + + public RegexReplace(string pattern, string replacement, RegexOptions regexOptions) + { + _regex = new Regex(pattern, regexOptions); + _replacementFormat = replacement; + } + + public RegexReplace(string pattern, MatchEvaluator replacement, RegexOptions regexOptions) + { + _regex = new Regex(pattern, regexOptions); + _replacementFunc = replacement; + } + + public string Replace(string input) + { + 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); + if (_replacementFunc != null) + input = _regex.Replace(input, _replacementFunc); + else + input = _regex.Replace(input, _replacementFormat); + return result; + } + } +} \ No newline at end of file