RegExp problem

C

Charlie S

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?
 
A

Alex Young

Charlie said:
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?
 
H

Harry Kakueki

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?
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

multiline regexp and newlines 2
Output confusion 2
too greedy of a regexp 3
regexp(ing) Backus-Naurish expressions ... 7
[ann] regexp-engine 0.11 2
regexp+hash problem 5
Help with code 0
regexp help needed 4

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top