[英]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.