mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-17 22:19:39 +02:00
JsonPartialMatcher - support Regex (#771)
* JsonPartialMatcher - support Regex * . * . * more tests * . * .
This commit is contained in:
@@ -1,24 +1,53 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using WireMock.RegularExpressions;
|
||||
|
||||
namespace WireMock.Util
|
||||
namespace WireMock.Util;
|
||||
|
||||
internal static class RegexUtils
|
||||
{
|
||||
internal static class RegexUtils
|
||||
private static readonly TimeSpan RegexTimeOut = new(0, 0, 10);
|
||||
|
||||
public static Dictionary<string, string> GetNamedGroups(Regex regex, string input)
|
||||
{
|
||||
public static Dictionary<string, string> GetNamedGroups(Regex regex, string input)
|
||||
var namedGroupsDictionary = new Dictionary<string, string>();
|
||||
|
||||
GroupCollection groups = regex.Match(input).Groups;
|
||||
foreach (string groupName in regex.GetGroupNames())
|
||||
{
|
||||
var namedGroupsDictionary = new Dictionary<string, string>();
|
||||
|
||||
GroupCollection groups = regex.Match(input).Groups;
|
||||
foreach (string groupName in regex.GetGroupNames())
|
||||
if (groups[groupName].Captures.Count > 0)
|
||||
{
|
||||
if (groups[groupName].Captures.Count > 0)
|
||||
{
|
||||
namedGroupsDictionary.Add(groupName, groups[groupName].Value);
|
||||
}
|
||||
namedGroupsDictionary.Add(groupName, groups[groupName].Value);
|
||||
}
|
||||
}
|
||||
|
||||
return namedGroupsDictionary;
|
||||
return namedGroupsDictionary;
|
||||
}
|
||||
|
||||
public static (bool IsValid, bool Result) MatchRegex(string pattern, string input, bool useRegexExtended = true)
|
||||
{
|
||||
if (string.IsNullOrEmpty(pattern))
|
||||
{
|
||||
return (false, false);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (useRegexExtended)
|
||||
{
|
||||
var r = new RegexExtended(pattern, RegexOptions.None, RegexTimeOut);
|
||||
return (true, r.IsMatch(input));
|
||||
}
|
||||
else
|
||||
{
|
||||
var r = new Regex(pattern, RegexOptions.None, RegexTimeOut);
|
||||
return (true, r.IsMatch(input));
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return (false, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user