I'm trying to write a script that counts the number of words, but, with some exceptions described using some regular expressions.
The script looks as follows:
number_of_words = 0
standalone_number = /\A[-+]?[0-9]*\.?[0-9]+\Z/
standalone_letter = /\A([\w+\-].?)+@[a-z0-9\-]+(\.[a-z]+)*\.[a-z]+\Z/
email_address = /\A([\w+\-].?)+@[a-z0-9\-]+(\.[a-z]+)*\.[a-z]+\Z/
text.each_line(){ |line| number_of_words = number_of_words + line.split.size {|word| word !~ standalone_number and word !~ standalone_letter and word !~ email_address } }
puts number_of_words
As you can see, I don't want to include standalone numbers, letters, or email addresses in the word count,
When I read a text file containing this information:
1 2 ruby email@email.com
I got a word count of 4
, while I was expecting to get 1
(ruby only included in the count).
What am I missing here?
Thanks.
EDIT
I fixed the "standalone_letter" regular expression as it was written by mistake similar to the "email_address" regular expression.
I have solve the issue using a solution I have added to the answers.