【发布时间】:2017-01-11 15:25:45
【问题描述】:
我有一个带有 "impportent words" 的 linq 列表,其中包含句子和单个单词。
我需要查找"sentence_to_search_for_importent_word"中的importent_words中是否有任何单词或句子
最后结果应该以数组或其他形式结束。
这是我在知道之前所做的事情..
List<string> importent_words = new List<string>() {"age", "what is", ".", "pages"}
string sentence_to_search_for_importent_words = "what is your age.";
我需要在一个句子中找到importent_words,并将所有匹配项输出到一个列表中
我尝试了这个,但它并没有真正完成这项工作
var pattern = new Regex(@"\w+");
var qa = pattern.Split(first_sentence.ToLower()).Where(w => importent_words.Contains(w));
它必须返回“age”而不是“ages”,它还应该找到“what is”而不仅仅是“what”
使用\w 似乎可以找到年龄,但它只输出"this" 而不是"this is"
问题似乎在于“这是”不止一个词。
【问题讨论】:
-
您在寻找
importent_words.Where(x => sentence.Contains(x))吗?为什么是正则表达式? -
我同意,使用正则表达式会带来第二个问题,你不是在寻找模式,所以基本检查会好十倍
-
我只是觉得正则表达式很快,任何解决方案都一样好也很棒