feat: Add MatchAny util method to RegexUtils that tries to match an input against a sequence of patterns.

This commit is contained in:
Austin Noska
2021-02-18 16:22:16 -05:00
parent b5542fb0d2
commit 204d2a475d

View File

@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using ICD.Common.Properties;
namespace ICD.Common.Utils
{
@@ -111,5 +113,34 @@ namespace ICD.Common.Utils
return Regex.Replace(input, pattern, evaluator, options);
}
/// <summary>
/// Returns the first successful match or the last failure.
/// </summary>
/// <param name="input"></param>
/// <param name="patterns"></param>
/// <returns></returns>
[NotNull]
public static Match MatchAny([NotNull] string input, [NotNull] IEnumerable<string> patterns)
{
if (input == null)
throw new ArgumentNullException("input");
if (patterns == null)
throw new ArgumentNullException("patterns");
Match last = null;
foreach (string pattern in patterns)
{
last = Regex.Match(input, pattern);
if (last.Success)
break;
}
if (last == null)
throw new InvalidOperationException("No patterns provided");
return last;
}
}
}