From: Toki Toki Date: 2008-05-03T06:54:07+09:00 Subject: Re: How to search words starting with a particular char? ThoML wrote: >> I need to search how many words are starting with a specific character > > A word border can be matched with \b. > > Using split + grep: > irb(main)> a = 'foo bar foo bar foo bar' > irb(main)> a.split(/\b/).grep(/^f\w*/) > => ["foo", "foo", "foo"] > irb(main)> a.split(/\b/).grep(/^f\w*/).size > => 3 > > Another solution would be to use scan: > irb(main)> a.scan(/\bf\w*/) > => ["foo", "foo", "foo"] > irb(main)> a.scan(/\bf\w*/).size > => 3 Thanks a lot for the helpful and fast answer. Best regards. -- Posted via http://www.ruby-forum.com/.