mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-20 23:41:19 +02:00
Regex.Match (#201)
This commit is contained in:
26
src/WireMock.Net/Util/IndexableDictionary.cs
Normal file
26
src/WireMock.Net/Util/IndexableDictionary.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace WireMock.Util
|
||||
{
|
||||
public class IndexableDictionary<TKey, TValue> : Dictionary<TKey, TValue>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the value associated with the specified index.
|
||||
/// </summary>
|
||||
/// <param name="index"> The index of the value to get.</param>
|
||||
/// <returns>The value associated with the specified index.</returns>
|
||||
public TValue this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
// get the item for that index.
|
||||
if (index < 0 || index > Count)
|
||||
{
|
||||
throw new KeyNotFoundException();
|
||||
}
|
||||
return Values.Cast<TValue>().ToArray()[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
24
src/WireMock.Net/Util/RegexUtils.cs
Normal file
24
src/WireMock.Net/Util/RegexUtils.cs
Normal file
@@ -0,0 +1,24 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace WireMock.Utils
|
||||
{
|
||||
internal static class RegexUtils
|
||||
{
|
||||
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())
|
||||
{
|
||||
if (groups[groupName].Captures.Count > 0)
|
||||
{
|
||||
namedGroupsDictionary.Add(groupName, groups[groupName].Value);
|
||||
}
|
||||
}
|
||||
|
||||
return namedGroupsDictionary;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user