C# Create array from MatchCollection

i need to create an multidimensional array from this regex result

MatchCollection match = Regex.Matches(input, @"( +|)[( +|)PC( +|)(( +|)name( +|)=" + ""(.*)"( +|),( +|)ip( +|)=( +|)"(.*)"( +|),( +|)subnet( +|)=( +|)"(.*)"( +|),( +|)gateway( +|)=( +|)"(.*)"" + @")]");

that regex will catch this string

[PC(name="PC1", ip="192.168.1.2", subnet="255.255.255.0", gateway="192.168.1.1")]

and what i need is create multidimensional array like this

[0]PC1 -> [0]192.168.1.2, [1]255.255.255.0, [2]192.168.1.1

i can get each value of matchcollection using for loop but the problem is creating the multidimensional array

i already google it about 2d array but there's no one work. i found one code but it can't be used by string

        int[][] i = new int[2][];
        i[0] = new int[3] { 1, 2, 3 };
        i[1] = new int[2] { 4, 5 };

i need 2d array like this array[0] -> PCName -> [0]IP, [1]Subnet, [2]Gateway


I have tried to create an object[][] from captured values

sample code

       string input = "[PC(name="PC1", ip="192.168.1.2", subnet="255.255.255.0", gateway="192.168.1.1")]n[PC(name="PC2", ip="192.168.1.3", subnet="255.255.255.0", gateway="192.168.1.1")]";

       MatchCollection matches = Regex.Matches(input, @"name=""(.*?)"".*ip=""(.*?)"".*subnet=""(.*?)"".*gateway=""(.*?)""");

       object[][] values = matches.OfType<Match>()
                           .Select(m => new object[] { m.Groups[1], m.Groups[2], m.Groups[3], m.Groups[4] })
                           .ToArray();

result

note that I have also changed the regex to create groups so that the values can be grouped in the match

try the regex here http://regex101.com/r/nM6lK8/2

链接地址: http://www.djcxy.com/p/92832.html

上一篇: 我应该使用字符串与正则表达式匹配或不

下一篇: C#从MatchCollection创建数组