aboutsummaryrefslogtreecommitdiff
path: root/challenge-284/jtimothyking/csharp/InputParsingExtensions.cs
blob: d048e85f5964787572be37f0e5a3a51cce7d1f2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Text.RegularExpressions;

namespace PwcLib;

public static class InputParsingExtensions
{
    /// <summary>
    ///    Parses a line of the form "@<symbol> = (1, 2, 3)" into an array of integers.
    /// </summary>
    public static int[] GetInts(this string line, string symbol = "ints")
    {
        var match = Regex.Match(line, $@"
            \A \s*
            \@{symbol} \s* = \s* \(
                ( \d+ (?: \s* , \s* \d+ )* )
            \)
            \s* \z
        ", RegexOptions.IgnorePatternWhitespace);
        if (!match.Success)
            throw new ArgumentException($"Invalid input: {line}", nameof(line));
        return match.Groups[1].Value.Split(", ").Select(int.Parse).ToArray();
    }
}