开发者

RegEx How to find text between two strings [duplicate]

This question already has answers here: 开发者_C百科 Regex Match all characters between two strings (16 answers) Closed 7 months ago.

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜