Forum: Ruby RegExp problem

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
1739e0896f13e7eae20e1c1fd24f51f0?d=identicon&s=25 Charlie S (Guest)
on 2007-05-17 14:35
I need to match a block of text as follows

Discard all lines which begin with //
Capture all lines in between as single strings


// section 1

bit more

// section 2

   more stuff

// section 3

and yet more

Should yeild 3 strings:
[ bit\n\nbit more\n\n]
[\n\n\n   more stuff\n\n]
[\n\n\nand yet more\n]

Can anyone help?

I've tried:

builds = s.scan(%r[\n//[^\n]*\n([^\n]*\n)+?]m)

but this yields
[ bit]

how can I make the group capture more than a line?
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-05-17 14:52
(Received via mailing list)
Charlie S wrote:
> bit more
> but this yields
> [ bit]
> [\n]
> [\n]
> how can I make the group capture more than a line?
=> ["", "\n bit\n\nbit more\n\n", "\n\n\n   more stuff\n\n", "\n\n\nand
yet more\n\n"]

Close enough?
2f4d4f9c35ea851bffb9a9cc2e086365?d=identicon&s=25 Harry Kakueki (Guest)
on 2007-05-17 14:57
(Received via mailing list)
On 5/17/07, Charlie S <> wrote:
> bit more
Try something like this.
You can clean it up as necessary.

s = "// section 1\nbit\n\nbit more\n\n// section 2\n\nmore stuff\n\n//
section 3\n\nand yet more"
s.split(/\/\/\s+section\s+\d+/).each {|x| p x}



A Look into Japanese Ruby List in English
This topic is locked and can not be replied to.