Ask a Question related to Ruby, Design and Development.
-
dblack@superlink.net #1
Re: Ruby idiom for all matches in a string<Pine.LNX.4.44.0310122003300.6283-100000@ool-4355dfae.dyn.optonline.net>
Hi --
On Mon, 13 Oct 2003, Gavin Sinclair wrote:
That won't do it; it only gets the first one. To get them all, you> 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
can use String#scan:
irb(main):008:0> xirb(main):009:0> x.scan(/A(B+)/)> "ABA ABBBA CBBA"
=> [["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: [email]dblack@superlink.net[/email]
work: [email]blackdav@shu.edu[/email]
Web: [url]http://pirate.shu.edu/~blackdav[/url]
dblack@superlink.net Guest
-
learning the "Ruby way"<Pine.LNX.4.44.0311201150530.4491-100000@ool-4355dfae.dyn.optonline.net><Pine.LNX.4.44.0311201137580.4435-100000@ool-4355dfae.dyn.optonline.net>
Hi -- On Fri, 21 Nov 2003 dblack@wobblini.net wrote: Just to clarify: "This" means my solution, not your question :-) David -
"stereotyping" (was: Strong Typing (Managing metadata aboutattribute types))<Pine.LNX.4.44.0311171402340.1133-100000@ool-4355dfae.dyn.optonline.net><Pine.LNX.4.44.0311181524130.2236-100000@ool-4355dfae.dyn.optonline.net>
Hi -- On Tue, 18 Nov 2003, Thien Vuong wrote: Class name checking doesn't ensure needed behavior. Actually, let me say first that while I... -
POLS ANT file pattern in Ruby<Pine.LNX.4.44.0310181631510.7967-100000@ool-4355dfae.dyn.optonline.net>
Hi -- On Sat, 18 Oct 2003, Robert Dawson wrote: Just eyeballing it, I would expect this assertion to be true. I guess that means I would... -
Prolly a simple question<Pine.LNX.4.44.0310111117330.29007-100000@ool-4355dfae.dyn.optonline.net><Pine.LNX.4.44.0310111103530.28872-100000@ool-4355dfae.dyn.optonline.net>
Whoops, that was a message from March I just replied to. We're getting another wave of old messages. David -- David Alan Black home:... -
Enumerable#inject is surprising me...<Pine.LNX.4.44.0310082145050.20002-100000@ool-4355dfae.dyn.optonline.net><Pine.LNX.4.44.0310082132090.19888-100000@ool-4355dfae.dyn.optonline.net>
On Thu, 9 Oct 2003 dblack@superlink.net wrote: Whoops, I mean how it gets into the second one at all. David -- David Alan Black



Reply With Quote

