From: "Marcel Molina Jr." Date: 2006-04-29T02:46:53+09:00 Subject: Re: Counting words On Sat, Apr 29, 2006 at 02:43:30AM +0900, Jamal Mazrui wrote: > I've research this but am still having trouble getting it right .... > Can someone give me code that counts the number of words in a string via > RegExp and MatchData objects? I think I'd like a word to be defined as > contiguous characters surrounded by white space (or the start/end of the > string), though am open to other interpretations. Here is a naive implementation: class String def words scan(/\b\S+\b/) end end 'this is a sentence with some words'.words => ["this", "is", "a", "sentence", "with", "some", "words"] 'this is a sentence with some words'.words.size => 7 marcel -- Marcel Molina Jr.