Reputation: 37
I have a linq list with "impportent words"
which holds sentences and single words.
I need to find if there is any word or sentences from importent_words which is found in "sentence_to_search_for_importent_word"
At last the result should end up in a array or something.
here is what i done until know..
List<string> importent_words = new List<string>() {"age", "what is", ".", "pages"}
string sentence_to_search_for_importent_words = "what is your age.";
I need to find the importent_words
in a sentence and get all the matches outputted to a list
I try this but it does not really do the job
var pattern = new Regex(@"\w+");
var qa = pattern.Split(first_sentence.ToLower()).Where(w => importent_words.Contains(w));
It have to return "age" not "ages" also it should find "what is" not only "what"
With \w
it seems to find age but it only output "this"
instead of "this is"
It seems like the problem is that "this is" is more than one word.
Upvotes: 0
Views: 750
Reputation: 498
Just in case you'd like an extendable regex solution. The matches need to be escaped for the regex pattern. You could utilize Regex.Escape() for this.
List<string> importent_words = new List<string>() { "age", "what is", @"\.", "ages", "bob" };
string sentence_to_search_for_importent_words = "what is your age. Frank";
string regexString = string.Join("|" , importent_words.ToArray());
Regex wordRegex = new Regex(regexString);
string[] result = wordRegex.Matches(sentence_to_search_for_importent_words)
.Cast<Match>()
.Select(m => m.Value)
.ToArray();
Upvotes: 0
Reputation: 43300
If all you're looking for is the important words that appear in your sentence then you can just use
importent_words.Where(x => sentence.Contains(x))
If this needs to be case insensitive then you can find a replacement for the Contains(x)
from the answers to Case insensitive 'Contains(string)' (or use sentence.ToLower()
as you show in your current regex attempt)
Upvotes: 4