From: dblack@... Date: 2003-10-13T09:07:36+09:00 Subject: Re: Ruby idiom for all matches in a string Hi -- On Mon, 13 Oct 2003, Gavin Sinclair wrote: > On Monday, October 13, 2003, 8:34:41 AM, David wrote: > > > What's the ruby idiom for all the matches in a string. > > > x="ABA ABBBA CBBA" > > /A(B+)/.match(x) > > > Ultimately, I want to get [ "B", "BBB" ] > > > Thanks > > > /A(B+)/.match(x).captures That won't do it; it only gets the first one. To get them all, you can use String#scan: irb(main):008:0> x > "ABA ABBBA CBBA" irb(main):009:0> x.scan(/A(B+)/) => [["B"], ["BBB"]] The reason you get an array-of-arrays (which you may have to flatten before it's useful) is that each scan (i.e., each application of the regex) generates a new array of captures. (This starts to look less cumbersome when you've got more than one capture.) Also, #scan takes a block, which can be handy. David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav