c# – Regex c # groups to array, or IList<string>

Question:

Actually, there is a pattern that works, but there is a problem, I just can't figure out how to get everything from the first group.

var r = new Regex(@"^himikogp?:\/\/*(\/[\w- ./?%&=]*\/\w+\/)*([{0-9}]\w+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
var m = r.Match(command);

Here is the line from which we get the groups: himikogp://install/app/987 I would like to form an IList<string> from the groups, or an array of strings. Thank you.

Answer:

You need to use the Matches method of the Regex class and loop over the result it returns:

string pattern = "^himikogp?:\/\/*(\/[\w- ./?%&=]*\/\w+\/)*([{0-9}]\w+)$";
foreach (Match match in Regex.Matches(command, pattern, RegexOptions.IgnoreCase))
         Console.WriteLine("{0} at position {1}", match.Value, match.Index);

Instead of Console.WriteLine do what you need, that is, add match.Value to the array.

Scroll to Top