none
Regex Expression help RRS feed

  • Question

  • Hi,

    Iam new to regex expressions.

    I want to see the number of occurances of , in a string .

    I tried several Regex expression like

    matchedString = Regex.Match(inputString, ","), Regex.Match(inputString, "(\.)")

    Its working but its only detecting first occurance of "," in the sense if i check the Length of matchedString, its always returning 1.

    How can i get the number of occurances of any letter in a given string

    Jerry

    Friday, October 18, 2013 7:22 AM

Answers

  • Hi Jerry,

    the method Regex.Match returns the first match of your pattern in the input string.

    If you want to count all the occurrences, you'll have to use the Regex.Matches method instead:

    var matches = Regex.Matches("sample string", "s");
    Console.WriteLine (matches.Count);

    HTH
    --mc

    Saturday, October 19, 2013 7:45 AM