RegEx How to find text between two strings [duplicate]
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.
Make it lazy - /begin.*?end/
Sidenote: "lazy" is no less acceptable than "non-greedy" is. Example, example, example
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
精彩评论