繁体   English   中英

检查字符串元组列表是否按顺序出现在字符串中

[英]Check if List of Tuples of Strings appear in string in order

我有一个元组列表,其中包含要在文本文件对象(newFile)的Queue属性中检查的字符串组合。 队列是称为行的字符串队列。

我不确定是否要去元组列表,但是我只想要一个真实的结果,如果在任何行中找到了任何元组的Item1和Item2(按Item1,然后按Item2顺序)。 这是我最好的镜头,我只是想不出如何编写LINQ语句。

List<Tuple<string,string>> codes = new List<Tuple<string, string>>()
   {
      new Tuple<string, string>("01,", "02,"),
      new Tuple<string, string>("02,", "03,"),
      new Tuple<string, string>("03,", "88,"),
      new Tuple<string, string>("88,", "88,"),
      new Tuple<string, string>("89,", "90,")                 
   };

bool codesFound = newFile.Lines
                      .Any(Regex.Match(codes.Select(x => (x.Item1 + "(.*)" + x.Item2)));

这样的事情应该会给您带来您想要的结果:

bool found = newFile.Lines
    .Any(x => codes.Select(y => x.IndexOf(y.Item1) > -1 && x.IndexOf(y.Item2) > -1 
                            && x.IndexOf(y.Item1) < x.IndexOf(y.Item2)).Any(z => z));

万一您想检查正则表达式的方式,这里是:

bool codesFound = newFile.Lines.Any(p =>
              Regex.IsMatch(p, string.Join("|", codes.Select(x => x.Item1 + ".+" + x.Item2).ToList()))
              );

在这里,我将所有模式连接到单个字符串中,例如01,.+02,|02,.+03, ...,然后检查输入数组中是否有满足此条件的字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM