mirror of
https://github.com/lidarr/Lidarr
synced 2025-02-25 23:32:41 +00:00
Changed: Simplified more RegexReplace instances
Co-Authored-By: taloth <taloth@users.noreply.github.com>
This commit is contained in:
parent
38448333b2
commit
d9744c4102
2 changed files with 72 additions and 21 deletions
|
@ -155,30 +155,35 @@ public static class Parser
|
|||
new Regex(@"^b00bs$", RegexOptions.Compiled | RegexOptions.IgnoreCase)
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
//TODO Rework this Regex for Music
|
||||
private static readonly Regex SimpleTitleRegex = new Regex(@"(?:(480|720|1080|2160|320)[ip]|[xh][\W_]?26[45]|DD\W?5\W1|[<>*:|]|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)((?<airyear>\d{4})[_.-](?<airmonth>[0-1][0-9])[_.-](?<airday>[0-3][0-9])|(?<airmonth>[0-1][0-9])[_.-](?<airday>[0-3][0-9])[_.-](?<airyear>\d{4}))(?!\d)",
|
||||
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(@"^(.*?[-._ ])|(-(RP|1|NZBGeek|Obfuscated|Scrambled|sample|Pre|postbot|xpost))+$",
|
||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
||||
private static readonly Regex CleanTorrentSuffixRegex = new Regex(@"\[(?:ettv|rartv|rarbg|cttv)\]$",
|
||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
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 RegexReplace CleanTorrentSuffixRegex = new RegexReplace(@"\[(?:ettv|rartv|rarbg|cttv)\]$",
|
||||
string.Empty,
|
||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
||||
private static readonly Regex ReleaseGroupRegex = new Regex(@"-(?<releasegroup>[a-z0-9]+)(?<!MP3|ALAC|FLAC|WEB)(?:\b|[-._ ])",
|
||||
RegexOptions.IgnoreCase | RegexOptions.Compiled);
|
||||
|
@ -240,12 +245,12 @@ public static ParsedTrackInfo ParseMusicTitle(string title)
|
|||
|
||||
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)
|
||||
|
@ -316,11 +321,11 @@ public static ParsedAlbumInfo ParseAlbumTitleWithSearchCriteria(string title, Ar
|
|||
|
||||
var releaseTitle = RemoveFileExtension(title);
|
||||
|
||||
var simpleTitle = SimpleTitleRegex.Replace(releaseTitle, string.Empty);
|
||||
var simpleTitle = SimpleTitleRegex.Replace(releaseTitle);
|
||||
|
||||
simpleTitle = WebsitePrefixRegex.Replace(simpleTitle, string.Empty);
|
||||
simpleTitle = WebsitePrefixRegex.Replace(simpleTitle);
|
||||
|
||||
simpleTitle = CleanTorrentSuffixRegex.Replace(simpleTitle, string.Empty);
|
||||
simpleTitle = CleanTorrentSuffixRegex.Replace(simpleTitle);
|
||||
|
||||
var escapedArtist = Regex.Escape(artist.Name.RemoveAccent()).Replace(@"\ ", @"[\W_]");
|
||||
var escapedAlbums = string.Join("|", album.Select(s => Regex.Escape(s.Title.RemoveAccent())).ToList()).Replace(@"\ ", @"[\W_]");
|
||||
|
@ -385,12 +390,12 @@ public static ParsedAlbumInfo ParseAlbumTitle(string title)
|
|||
|
||||
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 @@ public static string CleanArtistName(this string name)
|
|||
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 @@ public static string ParseReleaseGroup(string title)
|
|||
{
|
||||
title = title.Trim();
|
||||
title = RemoveFileExtension(title);
|
||||
title = WebsitePrefixRegex.Replace(title, "");
|
||||
title = WebsitePrefixRegex.Replace(title);
|
||||
|
||||
var animeMatch = AnimeReleaseGroupRegex.Match(title);
|
||||
|
||||
|
@ -509,7 +514,7 @@ public static string ParseReleaseGroup(string title)
|
|||
return animeMatch.Groups["subgroup"].Value;
|
||||
}
|
||||
|
||||
title = CleanReleaseGroupRegex.Replace(title, "");
|
||||
title = CleanReleaseGroupRegex.Replace(title);
|
||||
|
||||
var matches = ReleaseGroupRegex.Matches(title);
|
||||
|
||||
|
|
46
src/NzbDrone.Core/Parser/RegexReplace.cs
Normal file
46
src/NzbDrone.Core/Parser/RegexReplace.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue