using System; using System.Collections.Generic; using System.Text.RegularExpressions; using WireMock.Validation; namespace WireMock.RegularExpressions { /// /// Extension to the object, adding support for GUID tokens for matching on. /// #if !NETSTANDARD1_3 [Serializable] #endif internal class RegexExtended : Regex { /// public RegexExtended(string pattern) : this(pattern, RegexOptions.None) { } /// public RegexExtended(string pattern, RegexOptions options) : this(pattern, options, Regex.InfiniteMatchTimeout) { } /// public RegexExtended(string pattern, RegexOptions options, TimeSpan matchTimeout) : base(ReplaceGuidPattern(pattern), options, matchTimeout) { } #if !NETSTANDARD1_3 /// protected RegexExtended(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { } #endif // Dictionary of various Guid tokens with a corresponding regular expression pattern to use instead. private static readonly Dictionary GuidTokenPatterns = new Dictionary { // Lower case format `B` Guid pattern { @"\guidb", @"(\{[a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\})" }, // Upper case format `B` Guid pattern { @"\GUIDB", @"(\{[A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}\})" }, // Lower case format `D` Guid pattern { @"\guidd", "([a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12})" }, // Upper case format `D` Guid pattern { @"\GUIDD", "([A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12})" }, // Lower case format `N` Guid pattern { @"\guidn", "([a-z0-9]{32})" }, // Upper case format `N` Guid pattern { @"\GUIDN", "([A-Z0-9]{32})" }, // Lower case format `P` Guid pattern { @"\guidp", @"(\([a-z0-9]{8}-([a-z0-9]{4}-){3}[a-z0-9]{12}\))" }, // Upper case format `P` Guid pattern { @"\GUIDP", @"(\([A-Z0-9]{8}-([A-Z0-9]{4}-){3}[A-Z0-9]{12}\))" }, // Lower case format `X` Guid pattern { @"\guidx", @"(\{0x[a-f0-9]{8},0x[a-f0-9]{4},0x[a-f0-9]{4},\{(0x[a-f0-9]{2},){7}(0x[a-f0-9]{2})\}\})" }, // Upper case format `X` Guid pattern { @"\GUIDX", @"(\{0x[A-F0-9]{8},0x[A-F0-9]{4},0x[A-F0-9]{4},\{(0x[A-F0-9]{2},){7}(0x[A-F0-9]{2})\}\})" }, }; /// /// Replaces all instances of valid GUID tokens with the correct regular expression to match. /// /// Pattern to replace token for. private static string ReplaceGuidPattern(string pattern) { Check.NotNull(pattern, nameof(pattern)); foreach (var tokenPattern in GuidTokenPatterns) { pattern = pattern.Replace(tokenPattern.Key, tokenPattern.Value); } return pattern; } } }