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
···
–
David Corbin dcorbin@machturtle.com
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
–
David Corbin dcorbin@machturtle.com
/A(B+)/.match(x).captures
Gavin
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
Hi –
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
On Mon, 13 Oct 2003, Gavin Sinclair wrote:
On Monday, October 13, 2003, 8:34:41 AM, David wrote:
–
David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav