0

Given I have the following information:

string Sentence = "The dog jumped over the cat and the cat jumped above the mouse."
string startword = "jumped"
string endword = "the"

My requirement is how to program in C# to count the number of occurrences that the Sentence contains the starting of the startword until matching the second endword.

The above example should return 2 because The dog [jumped] ... [the] cat and ...cat [jumped] .. [the] mouse.

One of my ideas of to do string.Split the Sentence into string of words and looping through the words and compare with startword. If startword matched, then compare the next word to the endword until found or end of Sentence. If the startword and endword have been found, increase the counter, and continue searching for startword and endword until end of sentence.

Any other suggestion or code sample will be appreciated.

Keshav Pradeep Ramanath
  • 1,623
  • 4
  • 24
  • 33
Andy
  • 21
  • 4

3 Answers3

2

I would suggest using regex for this.

The regex to use is pretty straightforward: jumped.*?the

You can play with it here: https://regex101.com/r/h0MKyV/1

Since you are expecting multiple matches, you can get them in C# using regex.Matches call. So your code should look like the following:

var regex = new Regex("jumped.*?the");
var str = "The dog jumped over the cat and the cat jumped above the mouse.";
var matches = regex.Matches(str);

You can iterate over the matches to access each match, or get their count directly.

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
  • 1
    Thank you so much. i only need the count. I saw other Regex examples, but i didn't know it can do wildcard search. – Andy Mar 15 '18 at 05:21
2

The answers here are good. However, I just thought I'd point out that you might want to respect word boundaries by using \b. Otherwise you might pick up partial words.

\b Matches any word boundary. Specifically, any point between a \w and a \W.

var regex = new Regex(@"\bjumped\b.*?\bthe\b");
var str = "The dog jumped over theatre cat and the cat bejumped above the mouse.";
var matches = regex.Matches(str);
Console.WriteLine("Count : " + matches.Count);
foreach (var match in matches)
{
   Console.WriteLine(match);
}

You can see the demo here

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
1
regexPattern= "jumped[\s\w]+?the"
searchText = "The dog jumped over the cat and the cat jumped above the mouse."

The above regex match your question. To count the number of occurrences use

regexCount = Regex.Matches(searchText, regexPattern).Count

AbhishekGowda28
  • 994
  • 8
  • 19