简体   繁体   English

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

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

I have a list of Tuples that contain combinations of strings that I want to check for in a Queue property of my text file object (newFile). 我有一个元组列表,其中包含要在文本文件对象(newFile)的Queue属性中检查的字符串组合。 The Queue is a queue of strings called Lines. 队列是称为行的字符串队列。

I'm not sure the list of Tuples is the way to go but I only want a true result if Item1 and Item2 of any Tuple are found (in Item1 then Item2 order) in any line. 我不确定是否要去元组列表,但是我只想要一个真实的结果,如果在任何行中找到了任何元组的Item1和Item2(按Item1,然后按Item2顺序)。 Here's my best shot and I just can't figure out how to write the LINQ statement. 这是我最好的镜头,我只是想不出如何编写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)));

Something like this should get you the result you're after: 这样的事情应该会给您带来您想要的结果:

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));

Just in case you want to check the regex way, here you are: 万一您想检查正则表达式的方式,这里是:

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

Here, I join all patterns into a single string like 01,.+02,|02,.+03, ... and then check if there is any string in the input array that satisfies this condition. 在这里,我将所有模式连接到单个字符串中,例如01,.+02,|02,.+03, ...,然后检查输入数组中是否有满足此条件的字符串。

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

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