I have this text and I want to capture
begin
text
end
begin
text
end
begin
text
end
the text between the begin and end.
/begin.*end/
this will capture the first begin and the last end.
If your text contains line feeds (\n or \r) you'll need to add the "dotall" flag to your regex, as well as make your match reluctant (ie "non greedy")
Depending on your regex flavour:
/begin.*?end/s
(?s)begin.*?end