| | | 1 | | using System; |
| | | 2 | | using System.Linq; |
| | | 3 | | using System.Text.RegularExpressions; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using WireMock.Validation; |
| | | 6 | | |
| | | 7 | | namespace WireMock.Matchers |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Regular Expression Matcher |
| | | 11 | | /// </summary> |
| | | 12 | | /// <inheritdoc cref="IStringMatcher"/> |
| | | 13 | | /// <inheritdoc cref="IIgnoreCaseMatcher"/> |
| | | 14 | | public class RegexMatcher : IStringMatcher, IIgnoreCaseMatcher |
| | | 15 | | { |
| | | 16 | | private readonly string[] _patterns; |
| | | 17 | | private readonly Regex[] _expressions; |
| | | 18 | | |
| | | 19 | | /// <inheritdoc cref="IMatcher.MatchBehaviour"/> |
| | 370 | 20 | | public MatchBehaviour MatchBehaviour { get; } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="RegexMatcher"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="pattern">The pattern.</param> |
| | | 26 | | /// <param name="ignoreCase">Ignore the case from the pattern.</param> |
| | 8 | 27 | | public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new[] { pattern }, i |
| | 8 | 28 | | { |
| | 8 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Initializes a new instance of the <see cref="RegexMatcher"/> class. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 35 | | /// <param name="pattern">The pattern.</param> |
| | | 36 | | /// <param name="ignoreCase">Ignore the case from the pattern.</param> |
| | 98 | 37 | | public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string pattern, bool ignoreCase = fal |
| | 98 | 38 | | { |
| | 98 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Initializes a new instance of the <see cref="RegexMatcher"/> class. |
| | | 43 | | /// </summary> |
| | | 44 | | /// <param name="patterns">The patterns.</param> |
| | | 45 | | /// <param name="ignoreCase">Ignore the case from the pattern.</param> |
| | 8 | 46 | | public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) : this(MatchBehaviour.Ac |
| | 8 | 47 | | { |
| | 8 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Initializes a new instance of the <see cref="RegexMatcher"/> class. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="matchBehaviour">The match behaviour.</param> |
| | | 54 | | /// <param name="patterns">The patterns.</param> |
| | | 55 | | /// <param name="ignoreCase">Ignore the case from the pattern.</param> |
| | 407 | 56 | | public RegexMatcher(MatchBehaviour matchBehaviour, [NotNull, RegexPattern] string[] patterns, bool ignoreCase = |
| | 408 | 57 | | { |
| | 408 | 58 | | Check.NotNull(patterns, nameof(patterns)); |
| | | 59 | | |
| | 408 | 60 | | _patterns = patterns; |
| | 408 | 61 | | IgnoreCase = ignoreCase; |
| | 407 | 62 | | MatchBehaviour = matchBehaviour; |
| | | 63 | | |
| | 408 | 64 | | RegexOptions options = RegexOptions.Compiled; |
| | 408 | 65 | | if (ignoreCase) |
| | 64 | 66 | | { |
| | 64 | 67 | | options |= RegexOptions.IgnoreCase; |
| | 64 | 68 | | } |
| | | 69 | | |
| | 818 | 70 | | _expressions = patterns.Select(p => new Regex(p, options)).ToArray(); |
| | 407 | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <inheritdoc cref="IStringMatcher.IsMatch"/> |
| | | 74 | | public double IsMatch(string input) |
| | 368 | 75 | | { |
| | 368 | 76 | | double match = MatchScores.Mismatch; |
| | 368 | 77 | | if (input != null) |
| | 367 | 78 | | { |
| | | 79 | | try |
| | 367 | 80 | | { |
| | 1103 | 81 | | match = MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input))); |
| | 367 | 82 | | } |
| | 0 | 83 | | catch (Exception) |
| | 0 | 84 | | { |
| | | 85 | | // just ignore exception |
| | 0 | 86 | | } |
| | 367 | 87 | | } |
| | | 88 | | |
| | 368 | 89 | | return MatchBehaviourHelper.Convert(MatchBehaviour, match); |
| | 368 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <inheritdoc cref="IStringMatcher.GetPatterns"/> |
| | | 93 | | public virtual string[] GetPatterns() |
| | 3 | 94 | | { |
| | 3 | 95 | | return _patterns; |
| | 3 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc cref="IMatcher.Name"/> |
| | 2 | 99 | | public virtual string Name => "RegexMatcher"; |
| | | 100 | | |
| | | 101 | | /// <inheritdoc cref="IIgnoreCaseMatcher.IgnoreCase"/> |
| | 3 | 102 | | public bool IgnoreCase { get; } |
| | | 103 | | } |
| | | 104 | | } |