[英]How to use RegEx in C# to take all the matching strings into a collection
我想将所有匹配项放入集合,或者至少将其值用空格分隔的新字符串。
var srcString = @"foo foo %%match1%%, foo %%match2%%, %%match3%% foo foo";
var output = Regex.Match(srcString, @"\%\%(.*)\%\%").Groups[1].Value;
其中输出必须是一个以“ match1”为元素,以“ match2”为下一个元素的集合,依此类推,或者至少是类似“ match1 match2 match3”的集合。
谢谢!
这应该可以解决问题
var srcString = @"foo foo %%match1%%, foo %%match2%%, %%match3%% foo foo";
IEnumberable<string> results = Regex.Matches(srcString, @"\%\%(.*?)\%\%").Cast<Match>().Select(match => match.Value);
尝试这个:
var srcString = @"foo foo %%match1%%, foo %%match2%%, %%match3%% foo foo";
var match = Regex.Match(srcString, @"%%([^%]+)%%");
while (match.Success)
{
Console.WriteLine(match.Groups[1].Value);
match = match.NextMatch();
}
输出:
match1
match2
match3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.