C#中是否有一个函数可以用正则表达式检查列表序列

C#中是否有一个函数可以用正则表达式检查列表序列,c#,regex,C#,Regex,我有两个列表,需要检查两个列表中项目的顺序是否与Regex相同 For instance I have one list with items {c1, s1?, s2?, s3}. Valid Sequence: {c1, s1, s2, s3}, {c1, s3}. Invalid Sequence: {c1, c1, s1, s2, s3}, {c1, s2, s1, s3},

我有两个列表,需要检查两个列表中项目的顺序是否与
Regex
相同

For instance I have one list with items {c1, s1?, s2?, s3}.

Valid Sequence:   {c1, s1, s2, s3}, 
                  {c1, s3}.

Invalid Sequence: {c1, c1, s1, s2, s3}, 
                  {c1, s2, s1, s3}, 
                  {c1},
                  {s3, c1}.

假设某类
分隔符
,例如
,“
从未出现在项中,我们可以构建正则表达式:

演示:

  List<string> list = new List<string>() {
    "c1", "s1?", "s2?", "s3"
  };

  Regex regex = ListToRegex(list);

  List<string>[] tests = new List<string>[] {
    new List<string>() {"c1", "s1", "s2", "s3"},
    new List<string>() {"c1", "s3"},
    new List<string>() {"c1", "c1", "s1", "s2", "s3"},
    new List<string>() {"c1", "s2", "s1", "s3"},
    new List<string>() {"c1" },
    new List<string>() {"s3", "c1" },
  };

  string report = string.Join(Environment.NewLine, tests
    .Select(test => new {
      valid = regex.IsMatch(string.Join(delimiter, test)) ? "Valid" : "Invalid",
      text = string.Join(", ", test)
    })
    .Select(test => $"{test.valid,-8} :: {test.text}"));

  Console.Write(report);
 Valid    :: c1, s1, s2, s3  
 Valid    :: c1, s3
 Invalid  :: c1, c1, s1, s2, s3
 Invalid  :: c1, s2, s1, s3
 Invalid  :: c1
 Invalid  :: s3, c1

据我所知,目前还没有这样的标准方法,请准备好对其进行编码秩序重要吗?也就是说,
{s3,c1}
是一个有效的序列吗?是的,顺序很重要。{s3,c1}应该无效
  List<string> list = new List<string>() {
    "c1", "s1?", "s2?", "s3"
  };

  Regex regex = ListToRegex(list);

  List<string>[] tests = new List<string>[] {
    new List<string>() {"c1", "s1", "s2", "s3"},
    new List<string>() {"c1", "s3"},
    new List<string>() {"c1", "c1", "s1", "s2", "s3"},
    new List<string>() {"c1", "s2", "s1", "s3"},
    new List<string>() {"c1" },
    new List<string>() {"s3", "c1" },
  };

  string report = string.Join(Environment.NewLine, tests
    .Select(test => new {
      valid = regex.IsMatch(string.Join(delimiter, test)) ? "Valid" : "Invalid",
      text = string.Join(", ", test)
    })
    .Select(test => $"{test.valid,-8} :: {test.text}"));

  Console.Write(report);
 Valid    :: c1, s1, s2, s3  
 Valid    :: c1, s3
 Invalid  :: c1, c1, s1, s2, s3
 Invalid  :: c1, s2, s1, s3
 Invalid  :: c1
 Invalid  :: s3, c1