Thanks in advance to all help on this.
I am rather new to regex and I am finding it a little difficult to master.
I have a WindowsForm C#
in which the user can enter a set of data in a comma separated format as a single string. The data is also grouped using brackets ().
Each group consists of
(int type, int year, int age_start, int age_end)
In the text box the user can enter multiple groups
(group1),(group2)
I have managed to get the regex for splitting the groups
Regex RejectStringRegex = new Regex(@"\(([^)]*)\)");
and to split the data in the groups (I just have realised this might not account for spaces in the text)
Regex SubRejectStringRegex = new Regex(@"(\d+),(\d+),(\d+),(\d+)");
What I can't get right is how to reject malformed group strings for example
(1,89,10,10),(
or
(1,14,10,10),(2,15,20,30),(10
The code to perform the check currently looks like this. Note that I separated the regex
checks as I am using them to process the data later into a List<>
however if it is simpler to do it as one process then I am fine with this as well.
private bool CheckForValidReject()
{
bool StringValid = false;
Regex RejectStringRegex = new Regex(@"\(([^)]*)\)");
Regex SubRejectStringRegex = new Regex(@"(\d+),(\d+),(\d+),(\d+)");
MatchCollection AllMatches = RejectStringRegex.Matches(tbRejectString.Text);
if (AllMatches.Count > 0)
{
StringValid = true;
foreach (Match SomeMatch in AllMatches)
{
Match RejectMatch = SubRejectStringRegex.Match(SomeMatch.Groups[0].Value);
if (!RejectMatch.Success)
{
StringValid = false;
}
}
}
return StringValid;
}
N.B: the text box is checked for empty string before calling.
You can use
^\(\s*(?<type>\d+)\s*,\s*(?<year>\d+)\s*,\s*(?<agestart>\d+)\s*,(?<ageend>\d+)\s*\)\s*(,\s*\(\s*(?<type>\d+)\s*,\s*(?<year>\d+)\s*,\s*(?<agestart>\d+)\s*,(?<ageend>\d+)\s*\)\s*)*$
See regex demo, use with RegexOptions.ExplicitCapture
flag.
In C#, you need to get the groups and then access all values via Captures
CaptureCollection.
Here is a demo:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
// And then...
var strs = new List<string> { "(1,89,10,10)", "(1,14,10,10),(2,13,11,12)"};
var pattern = @"^\(\s*(?<type>\d+)\s*,\s*(?<year>\d+)\s*,\s*(?<agestart>\d+)\s*,(?<ageend>\d+)\s*\)\s*(,\s*\(\s*(?<type>\d+)\s*,\s*(?<year>\d+)\s*,\s*(?<agestart>\d+)\s*,(?<ageend>\d+)\s*\)\s*)*$";
foreach (var s in strs)
{
var match = Regex.Match(s, pattern, RegexOptions.ExplicitCapture);
if (match.Success)
{
Console.WriteLine(string.Join(", and ", match.Groups["type"].Captures.Cast<Capture>().Select(m => m.Value).ToList()));
Console.WriteLine(string.Join(", and ", match.Groups["year"].Captures.Cast<Capture>().Select(m => m.Value).ToList()));
Console.WriteLine(string.Join(", and ", match.Groups["agestart"].Captures.Cast<Capture>().Select(m => m.Value).ToList()));
Console.WriteLine(string.Join(", and ", match.Groups["ageend"].Captures.Cast<Capture>().Select(m => m.Value).ToList()));
}
}
See IDEONE demo
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments