You may want to split the sentence into words (let's decribe word as a nonempty sequence of letters or/and apostrophes - [\w']+
):
String sen = "hi, how are you?";
// ["hi", "how", "are", "you"]
String[] words = Regex
.Matches(sen, @"[\w']+")
.OfType<Match>()
.Select(match => match.Value)
.ToArray();
And then filter out all words that are in Greet
:
HashSet<string> greet = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"hi", "hello", "hey" };
List<string> matches = words
.Where(word => greet.Contains(word))
.ToList();
Or if we combine both parts:
HashSet<string> greet = new HashSet<string>(StringComparer.OrdinalIgnoreCase) {
"hi", "hello", "hey" };
List<string> matches = Regex
.Matches(sen, @"[\w']+")
.OfType<Match>()
.Select(match => match.Value)
.Where(word => greet.Contains(word))
.ToList();
Please, nocice that HashSet<T>
is often more convenient than T[]
when we want to perform Contain
.