Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 特定列表的正则表达式_C#_Regex_List_Numbers_Comma - Fatal编程技术网

C# 特定列表的正则表达式

C# 特定列表的正则表达式,c#,regex,list,numbers,comma,C#,Regex,List,Numbers,Comma,我需要用逗号分隔的数字(C#)从每一行中提取 我想有一个组名测试,将有两个匹配 test 35,1 35,2 35,3 35,4 35,5 test 35,1 35,2 35,3 35,4 35,5 到目前为止我所取得的成就: (?>测试(?>(?[\w\s,]+)\n)),但所有文本都被选中,直到最后一行 谢谢您可以这样命名您的捕获组:(?表达式)。剩下的写起来相当简单。以文本字符串test开头,后跟任何空格字符,以确保不捕获tes

我需要用逗号分隔的数字(C#)从每一行中提取

我想有一个组名测试,将有两个匹配

test    35,1    35,2    35,3    35,4    35,5
test    35,1    35,2    35,3    35,4    35,5
到目前为止我所取得的成就:
(?>测试(?>(?[\w\s,]+)\n))
,但所有文本都被选中,直到最后一行


谢谢

您可以这样命名您的捕获组:
(?表达式)
。剩下的写起来相当简单。以文本字符串
test
开头,后跟任何空格字符,以确保不捕获
test2
test3
。然后捕获所有剩余的字符以获取行的其余部分

(?<test>test\s.*)
(?测试*)
然后,您可以通过以下方式访问命名组:

var matches = Regex.Matches(input, @"(?<test>test\s.*)");
foreach(Match match in matches)
{
    string result = match.Groups["test"].Value;
}
var matches=Regex.matches(输入@“(?test\s.*);
foreach(匹配中的匹配)
{
字符串结果=match.Groups[“test”].Value;
}

以下是您可以利用的正则表达式:

(?<key>test\d*)\b(?>\s*(?<test>\d+(?:,\d+)*))+
输出:

test
35,1, and 35,2, and 35,3, and 35,4, and 35,5
test2
35,1, and 35,2, and 35,3, and 35,4, and 35,5
test3
35,1, and 35,2, and 35,3, and 35,4, and 35,5
test
35,1, and 35,2, and 35,3, and 35,4, and 35,5
test2
35,1, and 35,2, and 35,3, and 35,4, and 35,5
test3
35,1, and 35,2, and 35,3, and 35,4, and 35,5

类似于?Life saver谢谢你能帮我用相同的格式为所有组test2和test3制作相同的正则表达式,这样我就可以捕获所有匹配项你只需在
测试之后添加
\d*
(?>测试\d*\b(?>\s*(?\d+)+)
-并且这些数字都将在第2组捕获集合中。这样可以工作,如果名称与这些不同?对不起,所有的问题,但正则表达式是新的meSorry的另一个问题,但你给我看的正则表达式只选择最后一个数字35,5。我需要一个完整的行,上面有这样的数字:test235,135135,235,435,5。你能帮我吗?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;


public class Test
{
    public static void Main()
    {
        var strs = new List<string> { "test    35,1    35,2    35,3    35,4    35,5",
        "test2   35,1    35,2    35,3    35,4    35,5",
        "test3   35,1    35,2    35,3    35,4    35,5",
        "test    35,1    35,2    35,3    35,4    35,5",
        "test2   35,1    35,2    35,3    35,4    35,5",
        "test3   35,1    35,2    35,3    35,4    35,5"};

        var pattern = @"(?<key>test\d*)\b(?>\s*(?<test>\d+(?:,\d+)*))+";
        foreach (var s in strs)
        {
            var match = Regex.Match(s, pattern, RegexOptions.ExplicitCapture);  
            if (match.Success) 
            {                     // DEMO
                var key = match.Groups["key"].Value;
                var tests = match.Groups["test"].Captures.Cast<Capture>().Select(m => m.Value).ToList();
                Console.WriteLine(key);
                Console.WriteLine(string.Join(", and ", tests));
            }
        }
    }
}
test
35,1, and 35,2, and 35,3, and 35,4, and 35,5
test2
35,1, and 35,2, and 35,3, and 35,4, and 35,5
test3
35,1, and 35,2, and 35,3, and 35,4, and 35,5
test
35,1, and 35,2, and 35,3, and 35,4, and 35,5
test2
35,1, and 35,2, and 35,3, and 35,4, and 35,5
test3
35,1, and 35,2, and 35,3, and 35,4, and 35,5