這裏是正則表達式,你可以利用:
(?<key>test\d*)\b(?>\s*(?<test>\d+(?:,\d+)*))+
見regex demo here,該key
命名組將舉行test
+數字值和test
組將保存密鑰後的所有數字在CaptureCollection
(match.Groups["test"].Captures
):
![enter image description here](https://i.stack.imgur.com/ejG1o.png)
這裏是你展示如何檢索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組捕獲集合中。 –