Find word which was the match

I have this code where I search a string for words from an array:

string wordlist = synonymslistbox.Items[q_index].ToString().Split(':')[0].Replace(',', ' ');

var pattern = new Regex(@"\W");

var qa = pattern.Split(first_sentence).Any(w => wordlist.Contains(w));

Now I would like to achieve two things which I cannot figure out how to do.

  1. Find out which word was a match. This code just returns true if found.
  2. When first_sentences is i like my banjo then it should NOT find the letter a in banjo as the word a. It should only read a as a single word when it is in a sentence like this: i like a big beer at the end of the afternoon.

Upvotes: 2

Views: 82

Answers (1)

Tyress
Tyress

Reputation: 3653

  1. find out witch word was a match. this one just returns true if found.

Instead of any, use where:

var qa = pattern.Split(first_sentence).Where(w => wordlist.Contains(w));
  1. when first_sentences is "i like my banjo" then it should NOT find a in banjo as the word a. it should only read a as a single word when it is in a sentence like this below "i like a big beer at the end of the afternoon"

Your wordlist should not be a string but a list or array of strings. Make sure it's a list that has "a" in it. Aside from that your code will work

Upvotes: 1

Related Questions