1

I tried to find a method to count a specific word in a string, and I found this.

using System.Text.RegularExpressions;

MatchCollection matches = Regex.Matches("hi, hi, everybody.", ",");
int cnt = matches.Count;
Console.WriteLine(cnt);

It worked fine, and the result shows 2. But when I change "," to ".", it shows 18, not the expected 1. Why?

MatchCollection matches = Regex.Matches("hi, hi, everybody.", ".");

and when I change "," to "(", it shows me an error! the error reads:

SYSTEM.ARGUMENTEXCEPTION - THERE ARE TOO MANY (...

I don't understand why this is happening

MatchCollection matches = Regex.Matches("hi( hi( everybody.", "(");

Other cases seem to work fine but I need to count "(".

viz
  • 1,247
  • 16
  • 27

3 Answers3

2

The first instance, with the ., is using a special character which has a different meaning in regular expressions. It is matching ALL of the characters you have; hence you getting a result of 18.

http://www.regular-expressions.info/dot.html

To match an actual "." character, you'll need to "escape" it so that it is read as a full-stop and not a special character.

MatchCollection matches = Regex.Matches("hi, hi, everybody.", "\.");

The same exists for the ( character. It's a special character that has a different meaning in terms of regular expressions and you will need to escape it.

MatchCollection matches = Regex.Matches("hi( hi( everybody.", "\(");

Looks like you're new to regular expressions so I'd suggest reading, the link I posted above is a good start.

HOWEVER!

If you are looking to just count ocurences in a string, you don't need regex.

How would you count occurrences of a string within a string?

If you're using .NET 3.5 you can do this in a one-liner with LINQ:

int cnt = source.Count(f => f == '(');

If you don't want to use LINQ you can do it with:

int cnt = source.Split('(').Length - 1;
Community
  • 1
  • 1
Syx
  • 485
  • 3
  • 10
1

The second parameter represents a pattern, not necessarily just a character to search for in your string, and the ( by itself is an invalid pattern.

You don't need Regex to count occurrences of a character. Just use LINQ's Count():

var input = "hi( hi( everybody.";

var occurrences = input.Count(x => x == '(');  // 2
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
1

( character is a special character which means start of a group. If you need to use ( as literal you need to escape it with \(. That should solve your problem.

Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189