Using Regex in C# to check for optional comma seperated repeating sequence

Adrian Game

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.

Wiktor Stribiżew

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.

enter image description here

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript regex for a comma seperated sequence of numbers

From Dev

Check if entries are seperated by comma

From Dev

RegEx for comma seperated list

From Dev

while loop check for two values seperated by comma

From Dev

Removing value from a comma seperated list with Regex

From Dev

Regex to allow a comma seperated list of codes

From Dev

Regex expression - one or multiple strings seperated by comma

From Dev

Regex for word space comma seperated numbers

From Dev

regex: matching a repeating sequence

From Dev

Regex comma as optional character

From Dev

MySql comma seperated values and using IN to select data

From Dev

Retrieving and using values from a comma seperated string

From Dev

Using regex to check comma's usage

From Dev

Using regex to check comma's usage

From Dev

Using .replace to check regex for space, comma and period?

From Dev

Capture optional second group of digits (non-repeating) using regex

From Dev

Regex not capturing repeating optional captures

From Java

Regex with comma delimited string seperated by pipe sign in .NET

From Dev

RegEx - Extract words that contains a substring, from a comma seperated string

From Dev

Regex got Splitting comma seperated String ignoring commas in double quotes

From Dev

RegEx - Extract words that contains a substring, from a comma seperated string

From Dev

Making a regex expression for adding single quotes in numbers comma seperated

From Dev

How to split a comma seperated string and insert into html using javascript

From Dev

Python parse comma seperated nested brackets using Pyparsing

From Dev

How to split a comma seperated string and insert into html using javascript

From Dev

How to print the numbers separately (comma-seperated) using Python?

From Dev

Parsing comma seperated data of varying types in c++

From Dev

Edit comma seperated fields

From Dev

C# - is it possible to check which optional regex group is matched?

Related Related

  1. 1

    Javascript regex for a comma seperated sequence of numbers

  2. 2

    Check if entries are seperated by comma

  3. 3

    RegEx for comma seperated list

  4. 4

    while loop check for two values seperated by comma

  5. 5

    Removing value from a comma seperated list with Regex

  6. 6

    Regex to allow a comma seperated list of codes

  7. 7

    Regex expression - one or multiple strings seperated by comma

  8. 8

    Regex for word space comma seperated numbers

  9. 9

    regex: matching a repeating sequence

  10. 10

    Regex comma as optional character

  11. 11

    MySql comma seperated values and using IN to select data

  12. 12

    Retrieving and using values from a comma seperated string

  13. 13

    Using regex to check comma's usage

  14. 14

    Using regex to check comma's usage

  15. 15

    Using .replace to check regex for space, comma and period?

  16. 16

    Capture optional second group of digits (non-repeating) using regex

  17. 17

    Regex not capturing repeating optional captures

  18. 18

    Regex with comma delimited string seperated by pipe sign in .NET

  19. 19

    RegEx - Extract words that contains a substring, from a comma seperated string

  20. 20

    Regex got Splitting comma seperated String ignoring commas in double quotes

  21. 21

    RegEx - Extract words that contains a substring, from a comma seperated string

  22. 22

    Making a regex expression for adding single quotes in numbers comma seperated

  23. 23

    How to split a comma seperated string and insert into html using javascript

  24. 24

    Python parse comma seperated nested brackets using Pyparsing

  25. 25

    How to split a comma seperated string and insert into html using javascript

  26. 26

    How to print the numbers separately (comma-seperated) using Python?

  27. 27

    Parsing comma seperated data of varying types in c++

  28. 28

    Edit comma seperated fields

  29. 29

    C# - is it possible to check which optional regex group is matched?

HotTag

Archive