2007
09.25
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.
Possibly Related Posts
word count c# (4), c# regex count (3), word count in c# (3), regex word count (2), c# word count (2), c# regular expression count words (2), regex c# word count (1), regex c# count (1), regex validator word count (1), regex c# code to limit word length (1), regex word count limit (1), regex word limit c# (1), regular expression word count (1), search about words and count c# (1), special word count in c# (1), use regex in c# for word count (1), vb net word count in regular expression (1), word and count in c# (1), word count c# dictionary (1), word count in c# tutorial (1), world count regular expression (1), net regex word count (1), net regex count tab (1), c# regex word (1), c# regex word count (1), c# count words (1), c# regular expression word length (1), c# word count regular expression (1), character limit in fckeditor in c# (1), character limit regex in c# (1)
If you are a Linux guy, you will be upset to know that you are required a .NET Passport to login at Adobe and utilize the DRM Activator for your CCDA 640-863. The Microsoft Certified Systems Engineer on Windows Server 2003 diploma 70-293 is designed for IT professionals who work in the characteristically multifaceted computing environment of medium-sized to large businesses. Candidates for 70-536 work on a group in a small or large development environment that employs Microsoft Visual Studio .NET 2003 Enterprise Developer. CompTIA’s A+ 220-602 "IT Technician" test is among those two tests needed to accomplish the A+ 2006 certification.
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