Concatenate Regex.Matches to a string

.net, c#, c#-4.0, linq, regex

Solution

You can try to convert your result into an array and apply the `string.Join` to put your string in flat here you must specify the `Match` type explicitly as the `MatchCollection` is a `non-generic IEnumerable` type

  var toarray = from Match match in matchCollection select match.Value;
                string newflatChain = string.Join(";", toarray); 

or if you want just one line you can do it like the following

string newflatChain = string.Join(";", from Match match in matchCollection select match.Value);

Problem

I have a string like this: ``` string str = "key1=1;main.key=go1;main.test=go2;key2=2;x=y;main.go23=go23;main.go24=test24"; ``` I use this pattern to extract all substrings that starts with `main.`: ``` Regex regex = new Regex("main.[^=]+=[^=;]+"); MatchCollection matchCollection = regex.Matches(str); ``` To concatenate the MatchCollection, I have tried this: ``` string flatchain = string.Empty; foreach (Match m in matchCollection) { flatchain = flatchain +";"+ m.Value; } ``` Is there a better way to do it using LINQ?

Original source