Generate all Permutations of text from a regex pattern in C#

后端 未结 3 656
Happy的楠姐
Happy的楠姐 2021-01-17 06:23

So i have a regex pattern, and I want to generate all the text permutations that would be allowed from that pattern.

Example:

var pattern = \"^My (?:         


        
3条回答
  •  礼貌的吻别
    2021-01-17 07:12

    Here's a sketch of a function I wrote to take a List of Strings and return a list of all the permutated possibilities: (taking on char from each)

    public static List Calculate(List strings) {
                List returnValue = new List();
                int[] numbers = new int[strings.Count];
                for (int x = 0; x < strings.Count; x++) {
                    numbers[x] = 0;
                }
                while (true) {
                    StringBuilder value = new StringBuilder();
                    for (int x = 0; x < strings.Count; x++) {
                        value.Append(strings[x][numbers[x]]);
                        //int absd = numbers[x];
                    }
                    returnValue.Add(value.ToString());
                    numbers[0]++;
                    for (int x = 0; x < strings.Count-1; x++) {
                        if (numbers[x] == strings[x].Length) {
                            numbers[x] = 0;
                            numbers[x + 1] += 1;
                        }
                    }
                    if (numbers[strings.Count-1] == strings[strings.Count-1].Length)
                        break;
    
                }
                return returnValue;
            }
    

提交回复
热议问题