Search code examples
c#arraysregexcountfind-occurrences

Count total occurences of elements from string array in a text


I have a

public static string[] words = {"word1","word2","word3"};

I want to count occurences of word1 + occurences of word2+ occurences of word3 in a string.

I tried

Regex.Matches(string, "word1").Count 

which works fine for a single word but i don't know how to search for all strings. I dont't want to use foreach because the array "words" can contains up to 25 strings. Thanks.


Solution

  • This is a more versatile way to do this.
    Regex gives you more control over the context of the words it finds.
    And, I'm guessing it's a lot faster, since it does it all in one shot
    without a lot of primitives manipulation.

    string[] words = { "word1", "word2", "word3" };
    Regex rx = new Regex(   @"(?is)(?:.*?\b(" + string.Join("|", words) +   @")\b)+");
    
    string strin = "There are some word3 and more words and word1 and more word3, again word1";
    
    Match m = rx.Match( strin );
    if ( m.Success )
        Console.WriteLine("Found {0} words", m.Groups[1].Captures.Count);
    

    Output

    Found 4 words


    The regex above uses the word boundary \b.
    Alternative boundary choice: Whitespace (?<!\S) (?!\S)