簡體   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