I notice from my traffic log that there was someone bumping to this blog searching for ‘C# Regex Word Count’. Since I never create such tutorial, so here you go.
The basic rule of a word would be it doesn’t contain any whitespaces (spaces, tabs, or newlines). So the easiest Regex would be:
[^\ ^\t^\n]+
Now to implement it using C#, It would be as follows:
public int WordCount(String input) { int result = 0; try { Regex rgx = new Regex(@"[^\\ ^\\t^\\n]+"); MatchCollection mcMatches = rgx.Matches(input); result = mcMatches.Count; } catch (Exception ex) { } return result; }
An improvement would be putting the Regex creation out of the function, this way we will not re-created the Regex everytime the function is called.
loading...
About Hardono
Incoming Search
algorithm, c#
So Dono love regex too. Happy to know. What is your ‘mahdzab’? POSIX, Perl, …?
Hehe … at the moment its C# and/or VB.NET, but I might switch allegiance to Phyton or Ruby for the cool factor