user3284302
user3284302

Reputation: 139

Finding number of instances of exact word of "x" in text

I'm working c# to find out number of instances of exact word of "x". For example:

List<string> words = new List<string> {"Mode", "Model", "Model:"};
Text= "This is Model: x Type: y aa: e";

I've used Regex:

for(i=0; i<words.count; i++)
{
    word= list[i]
    int count= Regex.Matches(Text,word) 
}

But its not working. The result of above code gave count=1 for every Mode, Model, and Model:. I want to have my count to be 0 for Mode, 0 for Model, but 1 for Model: that it finds the number of instance of exact word.

Forgot that I can't use split in my case. Is there any way I can get not using split?

Upvotes: 2

Views: 153

Answers (3)

Salah Akbari
Salah Akbari

Reputation: 39956

I use LINQ for this purpose:

List<string> words = new List<string> { "Mode", "Model", "Model:" };
Text = "This is Model: x Type: Model: y aa: Mode e Model:";
var textArray = Text.Split(' ');
var countt = words.Select(item => textArray.ToList().Contains(item) ? 
             textArray.Count(d => d == item) : 0).ToArray();

Result:

For Mode => count = 1

For Model => count = 0

For Model: => count = 3

EDIT: I prefer to use LINQ for this purpose because as you see it is more easier and cleaner in this scenario, but if you are looking for a Regex solution yet you could try this:

List<int> count = new List<int>();
foreach (var word in words)
{
    var regex = new Regex(string.Format(@"\b{0}(\s|$)", word), RegexOptions.IgnoreCase);
    count.Add(regex.Matches(Text).Count);
}

EDIT2: Or by combining LINQ and Regex and without Split you can:

List<int> count = words.Select(word => new Regex(string.Format(@"\b{0}(\s|$)", word), RegexOptions.IgnoreCase))
                               .Select(regex => regex.Matches(Text).Count).ToList();

Upvotes: 5

Jcl
Jcl

Reputation: 28272

Although @S.Akhbari 's solution works... I think using Linq is cleaner:

var splitted = Text.Split(' ');
var items = words.Select(x => new { Word = x, Count = splitted.Count(y => y == x) });

Each item will have Word and Count properties.

See it in action here

Upvotes: 2

Midhun Mundayadan
Midhun Mundayadan

Reputation: 3182

\b matches on word boundaries.

 for(i=0; i<words.count; i++)
 {
     word= list[i] 
     var regex = new Regex(string.Format(@"\b{0}\b", word), 
                      RegexOptions.IgnoreCase);
     int count= regex.Matches(Text).Count; 
  }

Upvotes: 0

Related Questions