小编200*_*L.B的帖子

将Regex.Matches连接到一个字符串

这是我在Stack上的第一个问题我有这样的字符串

string str = "key1=1;main.key=go1;main.test=go2;key2=2;x=y;main.go23=go23;main.go24=test24";
Run Code Online (Sandbox Code Playgroud)

应用匹配模式以提取以main开头的所有字符串.返回

Regex regex = new Regex("main.[^=]+=[^=;]+");       
MatchCollection matchCollection = regex.Matches(str);
Run Code Online (Sandbox Code Playgroud)

我试过这个来连接匹配集合

string flatchain = string.Empty;
foreach (Match m in matchCollection)
{
    flatchain = flatchain +";"+ m.Value; 
}
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法来使用LINQ?

.net c# regex linq c#-4.0

5
推荐指数
2
解决办法
5248
查看次数

标签 统计

.net ×1

c# ×1

c#-4.0 ×1

linq ×1

regex ×1