mirror of
https://github.com/Sonarr/Sonarr
synced 2024-12-25 09:18:34 +00:00
Simplified more RegexReplace instances.
This commit is contained in:
parent
ff994d594a
commit
e5632019db
2 changed files with 46 additions and 20 deletions
|
@ -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|_)(?<!^)(a(?!$)|an|the|and|or|of)(?:\b|_))|\W|_",
|
||||
private static readonly RegexReplace NormalizeRegex = new RegexReplace(@"((?:\b|_)(?<!^)(a(?!$)|an|the|and|or|of)(?:\b|_))|\W|_",
|
||||
string.Empty,
|
||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
||||
private static readonly Regex FileExtensionRegex = new Regex(@"\.[a-z0-9]{2,4}$",
|
||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
||||
private static readonly Regex SimpleTitleRegex = new Regex(@"(?:(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*?",
|
||||
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(@"(?<=[_.-])(?<airdate>(?<!\d)(?<airyear>[1-9]\d{1})(?<airmonth>[0-1][0-9])(?<airday>[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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue