mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +00:00
feat: Add MatchAny util method to RegexUtils that tries to match an input against a sequence of patterns.
This commit is contained in:
@@ -1,7 +1,9 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
|
using ICD.Common.Properties;
|
||||||
|
|
||||||
namespace ICD.Common.Utils
|
namespace ICD.Common.Utils
|
||||||
{
|
{
|
||||||
@@ -111,5 +113,34 @@ namespace ICD.Common.Utils
|
|||||||
|
|
||||||
return Regex.Replace(input, pattern, evaluator, options);
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user