# RegExp problem

**URL:** <https://www.ruby-forum.com/t/regexp-problem/104392>\
**Category:** Ruby\
**Created:** [May 17, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/regexp-problem/104392 "2007-05-17T14:35:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Charlie\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@Charlie\_S](https://www.ruby-forum.com/u/Charlie_S)\
**Post date:** [May 17, 2007, 2:35pm UTC](https://www.ruby-forum.com/t/regexp-problem/104392/1 "2007-05-17T14:35:21Z")

</div>

I need to match a block of text as follows

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

So

// section 1  
bit

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]  
[\n]  
[\n]

how can I make the group capture more than a line?

---

<div class="post-metadata">

**Author:** ![Charlie\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@Charlie\_S](https://www.ruby-forum.com/u/Charlie_S)\
**Post date:** [May 17, 2007, 2:52pm UTC](https://www.ruby-forum.com/t/regexp-problem/104392/2 "2007-05-17T14:52:19Z")

</div>

Charlie S wrote:

> bit more
> 
> but this yields  
> [bit]  
> [\n]  
> [\n]
> 
> how can I make the group capture more than a line?

s.split(%r{//.\*})  
=\> ["", “\n bit\n\nbit more\n\n”, “\n\n\n more stuff\n\n”, “\n\n\nand  
yet more\n\n”]

Close enough?

---

<div class="post-metadata">

**Author:** ![Charlie\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c68b51/32.png) [@Charlie\_S](https://www.ruby-forum.com/u/Charlie_S)\
**Post date:** [May 17, 2007, 2:57pm UTC](https://www.ruby-forum.com/t/regexp-problem/104392/3 "2007-05-17T14:57:19Z")

</div>

On 5/17/07, Charlie S [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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}

Harry

–

A Look into Japanese Ruby List in English  
[http://www.kakueki.com/](http://www.kakueki.com/)
