[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

RegExp problem

Charlie S

5/17/2007 12:35:00 PM

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?

--
Posted via http://www.ruby-....

2 Answers

Alex Young

5/17/2007 12:51:00 PM

0

Charlie S wrote:
> 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?
>
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?

--
Alex

Harry Kakueki

5/17/2007 12:57:00 PM

0

On 5/17/07, Charlie S <rubyforum@skilbeck.com> wrote:
> 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?
>
> --
> Posted via http://www.ruby-....
>
>
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.ka...