views:

24

answers:

2

I have two groups of text:

firstgroup
(some content)
endgroup


secondgroup
(some content)
endgroup

I'm trying to just capture the first group (not the second group). I'm using this regular expression:

firstgroup(.|\n)*endgroup

For some reason, that regular expressions selects both first group and second group (probably because it looks at the very last "endgroup" above). Is there a way to modify my regex such that I only select the first group (and not the second group)?

+1  A: 

You need a lazy quantifier

/firstgroup\n(.*?)\nendgroup/m

to end the group as soon as possible. See http://www.rubular.com/r/D6UkOnMYLj.

(And you could use the /m flag to make the . match new lines.)

KennyTM
Great! Thanks Kenny :) That was fast! Thank you so much.
sjsc
+1  A: 
string=<<EOF
firstgroup
(some content)
endgroup


secondgroup
(some content)
endgroup
EOF

puts string.split(/endgroup/)[0] + "endgroup"