-1

I'm using the following regular expression to count the occurrence of a word inside a text

string term="framework";
int count = Regex.Matches(r.ResourceDescription.ToLower(), "\b" + term + "\b/gm").Count;

but on the following text (contained in r.ResourceDescription) it returns alway 0:

"Markets play a paramount role in enhancing people's livelihoods and promoting food security. BE tailored a food security framework having markets as one of the elements that should be tackled within the overall food security analysis."

Giox
  • 4,785
  • 8
  • 38
  • 81

1 Answers1

4

When you were creating a pattern, you forgot to escape string. So, try like this:

string desc = "Markets play a paramount role in enhancing people's livelihoods and promoting food security. BE tailored a food security framework having markets as one of the elements that should be tackled within the overall food security analysis";
string term = "framework";
string regex = @"\b" + term + @"\b";
int count = Regex.Matches(desc, regex, RegexOptions.Multiline | RegexOptions.IgnoreCase).Count;
Nino
  • 6,931
  • 2
  • 27
  • 42