using System;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
using WireMock.Validation;
namespace WireMock.Matchers
{
///
/// Regular Expression Matcher
///
///
public class RegexMatcher : IMatcher
{
private readonly string _pattern;
private readonly Regex _expression;
///
/// Initializes a new instance of the class.
///
/// The pattern.
/// IgnoreCase
public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false)
{
Check.NotNull(pattern, nameof(pattern));
_pattern = pattern;
RegexOptions options = RegexOptions.Compiled;
if (ignoreCase)
options |= RegexOptions.IgnoreCase;
_expression = new Regex(_pattern, options);
}
///
/// Determines whether the specified input is match.
///
/// The input.
///
/// true if the specified input is match; otherwise, false.
///
public bool IsMatch(string input)
{
if (input == null)
return false;
try
{
return _expression.IsMatch(input);
}
catch (Exception)
{
return false;
}
}
///
/// Gets the pattern.
///
/// Pattern
public string GetPattern()
{
return _pattern;
}
}
}