mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-04-12 03:57:32 +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:
parent
b5542fb0d2
commit
204d2a475d
1 changed files with 31 additions and 0 deletions
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue