Hi
Im working through the “Best of ruby quiz” book which some of you
might be familiar with, but hey dont worry if not, you can probably
still help me - I’ve found a regular expression that does what I
want, but not quite sure why it works.
Given:
story = “The ((velocity)) ((colour)) ((wildbeast)) ((action)) over the
((adjective)) ((domesticbeast))”
I want to parse this into an array such that each element of the array
is the string split on the “((blabla))” bits.
This does that:
irb(main):052:0> story.split /((.*?))/
=> ["The ", " ", " ", " ", " over the ", " "]
However I also want the sections marked “((blabla))” included as
well… I fiddled a bit and got this, which works:
irb(main):053:0> story.split /(((.*?)))+/
=> ["The ", “((velocity))”, " ", “((colour))”, " ", “((wildbeast))”, "
", “((action))”, " over the ", “((adjective))”, " ",
“((domesticbeast))”]
However Im not exactly sure what makes this work - can anyone
illuminate this for me?
glenn