Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have a long string of characters as input and I want to count the number of words in that string. How can I do it through regular expression?
Posted

Try:

C#
Regex word = new Regex(@"\w+");
string data = "The quick brown fox jumps over the lazy dog.";
Console.WriteLine(word.Matches(data).Count);


Get a copy of Expresso [^] - it's free, and it examines and generates Regular expressions.
 
Share this answer
 
Comments
Member 8491154 26-May-12 7:49am    
Any other tool like Expresso?
OriginalGriff 26-May-12 7:56am    
Probably, but that's the one I use, because it works for me!
Shahin Khorshidnia 26-May-12 12:05pm    
+5 Excellent
C#
string Input;
Input=Console.ReadLine();
string[] Output=Input.Split(' ');
Console.WriteLine("No of Words in Given String is {0}",Output.Length.ToString());
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900