sjsc sjsc - 1 year ago 82
Ruby Question

Regular expressions: How do I grab a block of text using regex? (in ruby)

I'm using ruby and I'm trying to find a way to grab text in between the {start_grab_entries} and {end_grab_entries} like so:

i want to grab
the text that
you see here in
the middle

Something like so:

$1 => "i want to grab
the text that
you see here in
the middle"

So far, I tried this as my regular expression:


However, using $1, that gives me a blank. Do you know what I can do to grab that block of text in between the tags correctly?

Answer Source

There is a better way to allow the dot to match newlines (/m modifier):

regexp = /\{start_grab_entries\}(.*?)\{end_grab_entries\}/m

Also, make the * lazy by appending a ?, or you might match too much if more than one such section occurs in your input.

That said, the reason why you got a blank match is that you repeated the capturing group itself; therefore you only caught the last repetition (in this case, a \n).

It would have "worked" if you had put the capturing group outside of the repetition:


but, as said above, there is a better way to do that.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download