RegexMatchValues

RegexMatchValues converts regular expression matches to strong types.

NuGet

Usage

The RegexMatchExtensions static class provides a few simple extension methods on Match that convert the capturing groups of the regular expression into a tuple. (Try it!)

var text = "Use 22/7 for pi.";
var (numerator, denominator) =
    Regex.Match(text, @"(\d+)/(\d+)").Get<(int, int)>();
Console.WriteLine((double) numerator / denominator);
// output: 3.142857142857143

Get<T>() throws InvalidOperationException if the match fails, so use one of the TryGet<T>() overloads if that is a possibility. (Try it!)

var text = "Use 3.14 for pi.";
if (Regex.Match(text, @"(\d+)/(\d+)").TryGet(out (int N, int D) fraction))
    Console.WriteLine((double) fraction.N / fraction.D);
else
    Console.WriteLine("No fraction found.");

Other types besides tuples are also supported. See the remarks in the documentation for the full details.