From: John Johnson Date: 2006-04-29T06:02:50+09:00 Subject: Re: Counting words One way is like this: irb(main):020:0> a="This is a test." => "This is a test." irb(main):021:0> a.scan(/\b\S.*?\b/).size => 4 irb(main):022:0> The Regexp in line 21 rewritten in a more readable form is: a.scan(/ \b (?# a word boundary ) \S (?# a character that is not a space ) .*? (?# maybe (*) some more characters (.), but don't be greedy (?)) \b (?# a word boundary ) /x btw, the Regexp above actually works because of the x at the end, meaning an extended regexp. Regards, JJ On Friday, April 28, 2006, at 04:35PM, 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. > >Jamal > > > --- Help everyone. If you can't do that, then at least be nice.