简体   繁体   English

在C#中使用Regex检查可选的逗号分隔重复序列

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

Thanks in advance to all help on this. 在此先感谢所有帮助。

I am rather new to regex and I am finding it a little difficult to master. 我对regex相当陌生,并且发现它很难掌握。

I have a WindowsForm C# in which the user can enter a set of data in a comma separated format as a single string. 我有一个WindowsForm C# ,用户可以在其中输入以逗号分隔格式的一组数据作为单个字符串。 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. 请注意,我在使用regex检查时将它们分开,以便稍后将数据处理到List<>但是如果将其作为一个过程进行处理比较简单,那么我也很满意。

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;
}

NB: 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. 请参阅regex演示 ,与RegexOptions.ExplicitCapture标志一起使用。

在此处输入图片说明

In C#, you need to get the groups and then access all values via Captures CaptureCollection. 在C#中,您需要获取组,然后通过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 IDEONE演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM