Вот регулярное выражение можно использовать:
(?<key>test\d*)\b(?>\s*(?<test>\d+(?:,\d+)*))+
Смотрите regex demo here, то key
именованной группа будет удерживать test
+ цифра (цифры), а группа test
будет содержать все числа после нажатия клавиши внутри CaptureCollection
(match.Groups["test"].Captures
):
Вот IDEONE демо показывает, как вы можете получить эти значения в C#:
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) & я = тест ++++ 35% 2c1 ++++ 35% 2c2 ++++ 35% 2c3 ++++ 35% 2C4 ++++ 35% 2C5% 0d% 0atest2 +++ 35 % 2c1 ++++ 35% 2c2 ++++ 35% 2 с3 ++++ 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)? –
Life saver Благодарю вас, пожалуйста, помогите мне сделать одно и то же регулярное выражение со всеми группами test2 и test3 с тем же форматом, чтобы я мог записывать все совпадения. – user3430435
Вы можете просто добавить '\ d *' после 'test':' (?> test \ d * \ b (?> \ s * (? \ d + (?:, \ d +) *)) +) '- и числа будут в коллекции захвата группы 2. –