mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-26 02:08:29 +02:00
support multiple patterns
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using JetBrains.Annotations;
|
||||
using WireMock.Validation;
|
||||
@@ -11,25 +12,34 @@ namespace WireMock.Matchers
|
||||
/// <seealso cref="IMatcher" />
|
||||
public class RegexMatcher : IMatcher
|
||||
{
|
||||
private readonly string _pattern;
|
||||
private readonly Regex _expression;
|
||||
private readonly string[] _patterns;
|
||||
private readonly Regex[] _expressions;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
|
||||
/// </summary>
|
||||
/// <param name="pattern">The pattern.</param>
|
||||
/// <param name="ignoreCase">IgnoreCase</param>
|
||||
public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false)
|
||||
public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new [] { pattern }, ignoreCase )
|
||||
{
|
||||
Check.NotNull(pattern, nameof(pattern));
|
||||
}
|
||||
|
||||
_pattern = pattern;
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RegexMatcher"/> class.
|
||||
/// </summary>
|
||||
/// <param name="patterns">The patterns.</param>
|
||||
/// <param name="ignoreCase">IgnoreCase</param>
|
||||
public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false)
|
||||
{
|
||||
Check.NotNull(patterns, nameof(patterns));
|
||||
|
||||
_patterns = patterns;
|
||||
|
||||
RegexOptions options = RegexOptions.Compiled;
|
||||
if (ignoreCase)
|
||||
options |= RegexOptions.IgnoreCase;
|
||||
|
||||
_expression = new Regex(_pattern, options);
|
||||
|
||||
_expressions = patterns.Select(p => new Regex(p, options)).ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -44,7 +54,7 @@ namespace WireMock.Matchers
|
||||
|
||||
try
|
||||
{
|
||||
return MatchScores.ToScore(_expression.IsMatch(input));
|
||||
return MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input)));
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
@@ -53,12 +63,12 @@ namespace WireMock.Matchers
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the pattern.
|
||||
/// Gets the patterns.
|
||||
/// </summary>
|
||||
/// <returns>Pattern</returns>
|
||||
public virtual string GetPattern()
|
||||
public virtual string[] GetPatterns()
|
||||
{
|
||||
return _pattern;
|
||||
return _patterns;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user