| | | 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 | | /// <seealso cref="IMatcher" /> |
| | | 13 | | public class RegexMatcher : IMatcher |
| | | 14 | | { |
| | | 15 | | private readonly string[] _patterns; |
| | | 16 | | private readonly Regex[] _expressions; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="RegexMatcher"/> class. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="pattern">The pattern.</param> |
| | | 22 | | /// <param name="ignoreCase">IgnoreCase</param> |
| | 39 | 23 | | public RegexMatcher([NotNull, RegexPattern] string pattern, bool ignoreCase = false) : this(new [] { pattern }, |
| | 39 | 24 | | { |
| | 39 | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="RegexMatcher"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="patterns">The patterns.</param> |
| | | 31 | | /// <param name="ignoreCase">IgnoreCase</param> |
| | 98 | 32 | | public RegexMatcher([NotNull, RegexPattern] string[] patterns, bool ignoreCase = false) |
| | 98 | 33 | | { |
| | 98 | 34 | | Check.NotNull(patterns, nameof(patterns)); |
| | | 35 | | |
| | 98 | 36 | | _patterns = patterns; |
| | | 37 | | |
| | 98 | 38 | | RegexOptions options = RegexOptions.Compiled; |
| | 98 | 39 | | if (ignoreCase) |
| | 4 | 40 | | options |= RegexOptions.IgnoreCase; |
| | | 41 | | |
| | 196 | 42 | | _expressions = patterns.Select(p => new Regex(p, options)).ToArray(); |
| | 98 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Determines whether the specified input is match. |
| | | 47 | | /// </summary> |
| | | 48 | | /// <param name="input">The input string</param> |
| | | 49 | | /// <returns>A value between 0.0 - 1.0 of the similarity.</returns> |
| | | 50 | | public double IsMatch(string input) |
| | 60 | 51 | | { |
| | 60 | 52 | | if (input == null) |
| | 0 | 53 | | return MatchScores.Mismatch; |
| | | 54 | | |
| | | 55 | | try |
| | 60 | 56 | | { |
| | 120 | 57 | | return MatchScores.ToScore(_expressions.Select(e => e.IsMatch(input))); |
| | | 58 | | } |
| | 0 | 59 | | catch (Exception) |
| | 0 | 60 | | { |
| | 0 | 61 | | return MatchScores.Mismatch; |
| | | 62 | | } |
| | 60 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Gets the patterns. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <returns>Pattern</returns> |
| | | 69 | | public virtual string[] GetPatterns() |
| | 0 | 70 | | { |
| | 0 | 71 | | return _patterns; |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Gets the name. |
| | | 76 | | /// </summary> |
| | | 77 | | /// <returns> |
| | | 78 | | /// Name |
| | | 79 | | /// </returns> |
| | | 80 | | public virtual string GetName() |
| | 0 | 81 | | { |
| | 0 | 82 | | return "RegexMatcher"; |
| | 0 | 83 | | } |
| | | 84 | | } |
| | | 85 | | } |