这里是正则表达式,你可以利用:
(?<key>test\d*)\b(?>\s*(?<test>\d+(?:,\d+)*))+
见regex demo here,该key
命名组将举行test
+数字值和test
组将保存密钥后的所有数字在CaptureCollection
(match.Groups["test"].Captures
):
这里是你展示如何检索C#这些值的IDEONE演示:
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
喜欢的东西[这里](HTTP: //regexstorm.net/tester?p=(%3f%3etest%5cb(%3f%3e%5cs*(%3f%3ctest%3e%5cd%2b(%3f%3a%2c%5cd%2b)*) )%2b)中与I =测试++++ 35%2C1 ++++ 35%2C2 ++++ 35%2C3 ++++ 35%2C4 ++++ 35%2C5%0D%0atest2 +++ 35 %2C1 ++++ 35%2C2 ++++ 35%2 C3 ++++ 35%2C4 ++++ 35%2C5%0D%0atest3 +++ 35%2C1 ++++ 35%2C2 ++++ 35%2C3 ++++ 35%2C4 ++++ 35%2C5%0D%0atest ++++ 35%2C1 ++++ 35%2C2 ++++ 35%2C3 ++++ 35%2C4 ++++ 35%2C5%0D%0atest2 +++ 35 %2C1 ++++ 35%2C2 ++++ 35%2C3 ++++ 35%2C4 ++++ 35%2C5%0D%0atest3 +++ 35%2C1 ++++ 35%2C2 +++ + 35%2C3 ++++ 35%2C4 ++++ 35%2C5)? –
救生员谢谢你能帮我做出与所有组测试test2和test3相同的正则表达式,以便我可以捕获所有匹配 – user3430435
你可以在'test'后添加'\ d *':( ?> test \ d * \ b(?> \ s *(? \ d +(?:,\ d +)*))+)' - 这些数字将全部在第2组捕获集合中。 –