2019-12-22 21:24:11 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2019-10-31 03:46:40 +00:00
|
|
|
|
|
|
|
|
|
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)
|
2019-12-22 22:08:53 +00:00
|
|
|
|
{
|
2019-10-31 03:46:40 +00:00
|
|
|
|
return _regex.Replace(input, _replacementFunc);
|
2019-12-22 22:08:53 +00:00
|
|
|
|
}
|
2019-10-31 03:46:40 +00:00
|
|
|
|
else
|
2019-12-22 22:08:53 +00:00
|
|
|
|
{
|
2019-10-31 03:46:40 +00:00
|
|
|
|
return _regex.Replace(input, _replacementFormat);
|
2019-12-22 22:08:53 +00:00
|
|
|
|
}
|
2019-10-31 03:46:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryReplace(ref string input)
|
|
|
|
|
{
|
|
|
|
|
var result = _regex.IsMatch(input);
|
|
|
|
|
if (_replacementFunc != null)
|
2019-12-22 22:08:53 +00:00
|
|
|
|
{
|
2019-10-31 03:46:40 +00:00
|
|
|
|
input = _regex.Replace(input, _replacementFunc);
|
2019-12-22 22:08:53 +00:00
|
|
|
|
}
|
2019-10-31 03:46:40 +00:00
|
|
|
|
else
|
2019-12-22 22:08:53 +00:00
|
|
|
|
{
|
2019-10-31 03:46:40 +00:00
|
|
|
|
input = _regex.Replace(input, _replacementFormat);
|
2019-12-22 22:08:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 03:46:40 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-12-22 21:24:10 +00:00
|
|
|
|
}
|