I have a program looking to capture formatted string input. The input looks something like
{1, 2, 3, 4, 5, 6, 7}
Where there can be a varying number of numbers as long as they are all inside the set. For instance:
{1, 2, 3}
{1, 2, 3, 4}
Would all be valid. However, I need to be able to access each number within that set. I have the following code
Match match = Regex.Match(input, @"\{(\d,\s)*(\d)\}", RegexOptions.IgnoreCase);
if (match.Success)
{
String s;
for(int i = 0; i < match.Groups.Count; i++)
{
s = match.Groups[i].Value;
// Do actions here
}
}
Which matches fine, however, I can only access the last and next-to-last number within the set. I would like to be able to read the values from each member of the set. How would I go about doing this? Would something other than regex work better?
While a regex would be most helpful for capturing the brace-enclosed strings, it would be easier after you get that to use simple imperative code to get the numbers within.
I would start with the regex
\{([^\}]*)\}to grab the inner part of any string starting with ‘{‘ and ending with ‘}’ (with no ‘}’ in between). You can then process the capture using a split by commas to get the numbers within, trimming for whitespace afterwards if needed.