1

I was looking for an answer for "how to count the occurrence of a substring in the desired string in c# using Regex?". There are many references like here to use "Count" as following:

int count = Regex.Match(MyString, "OU=").Count

I tried to use it but every time in c# there was an error message and a suggestion to use ".Length".

Eventually, I did it using the following code snippet.

    private int GetCounts(string source, string substring)
    {
        int iCount = 0;
        foreach (Match match in Regex.Matches(source, substring))
            iCount++;
        return iCount;
    }

Could anybody explain why ".Count" is not working as expected? Answers appreciated in advance.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
amirfg
  • 272
  • 2
  • 6
  • 21
  • 1
    Please provide the error message – Avi Meltser May 16 '19 at 13:57
  • Hi. Welcome to [so]. Help us help you. Can you please provide at least a sample test, tell how it does not match your requirements. Ideally provide a [mcve]. – PJProudhon May 16 '19 at 13:59
  • are you using a framework version below 4.8 ? Because this property exists and I can compile your first line of code without problems. Here is [the documentation](https://learn.microsoft.com/en-us/dotnet/api/system.text.regularexpressions.matchcollection.count?view=netframework-4.8) – Mong Zhu May 16 '19 at 14:01
  • 1
    @MongZhu `MatchCollection.Count` has been available since 1.1 – canton7 May 16 '19 at 14:03
  • Cannot reproduce: https://dotnetfiddle.net/ywGttu – canton7 May 16 '19 at 14:04
  • 1
    @canton7 haha thank you, I was looking at the wrong place for the version. stupid :D – Mong Zhu May 16 '19 at 14:04
  • Please show your RegEx and the string leading to the error. The only reason why `.Count` would fail (that is, with a `RegexMatchTimeoutException`) is when the RegEx evaluation takes to long, indicating an incorrect RegEx. – Thomas Flinkow May 16 '19 at 14:06
  • 1
    @ThomasFlinkow He said "*I tried to use it but every time in c# there was an error message and a suggestion to use ".Length".*", indicating that it's not a problem with a timeout, but instead it's a compile-time error. Also he did show us his Regex string: `"OU="` – canton7 May 16 '19 at 14:07
  • 2
    @canton7 Yes. Exactly. More interestingly somebody voted down the question when you guys are trying to help. Thanks to all you, guys.:-) – amirfg May 16 '19 at 14:10
  • 3
    @amirfg It's being downvoted because we cannot reproduce it. The code you gave us does not cause the error you say it causes. That makes it a bad question. – canton7 May 16 '19 at 14:11
  • 1
    I took the liberty and changed the code to make the problem reproducible – Mong Zhu May 16 '19 at 14:35

1 Answers1

4

You likely used Regex.Match when you should have used Regex.Matches.

The first returns a Match, which only has a Length property, while the latter returns a MatchCollection only having a Count property.

Thomas Flinkow
  • 4,845
  • 5
  • 29
  • 65
  • 3
    (@amirfg This is why it's important that your question is correct. Please always make sure that the code in your question is actually the code you're trying to run) – canton7 May 16 '19 at 14:16