Reputation: 4662
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)?
Upvotes: 0
Views: 1403
Reputation: 343135
string=<<EOF
firstgroup
(some content)
endgroup
secondgroup
(some content)
endgroup
EOF
puts string.split(/endgroup/)[0] + "endgroup"
Upvotes: 1
Reputation: 523764
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.)
Upvotes: 1