How to check if any word in my List contains in text

后端 未结 4 785
抹茶落季
抹茶落季 2020-12-16 12:39

I have a

List words = new List {\"word1\", \"word2\", \"word3\"};

And i want to check using linq if my string

4条回答
  •  囚心锁ツ
    2020-12-16 13:22

    You can use a simple LINQ query, if all you need is to check for substrings:

    var q = words.Any(w => myText.Contains(w));
    // returns true if myText == "This password1 is weak";
    

    If you want to check for whole words, you can use a regular expression:

    1. Matching against a regular expression that is the disjunction of all the words:

      // you may need to call ToArray if you're not on .NET 4
      var escapedWords = words.Select(w => @"\b" + Regex.Escape(w) + @"\b");
      // the following line builds a regex similar to: (word1)|(word2)|(word3)
      var pattern = new Regex("(" + string.Join(")|(", escapedWords) + ")");
      var q = pattern.IsMatch(myText);
      
    2. Splitting the string into words with a regular expression, and testing for membership on the words collection (this will get faster if you use make words into a HashSet instead of a List):

      var pattern = new Regex(@"\W");
      var q = pattern.Split(myText).Any(w => words.Contains(w));
      

    In order to filter a collection of sentences according to this criterion all you have to do is put it into a function and call Where:

     // Given:
     // bool HasThoseWords(string sentence) { blah }
     var q = sentences.Where(HasThoseWords);
    

    Or put it in a lambda:

     var q = sentences.Where(s => Regex.Split(myText, @"\W").Any(w => words.Contains(w)));
    

提交回复
热议问题