I suggest
val rg =s1.stripMargin('|').replaceAll("(?s)& Hello Buddy(.*?)&endmarker&" ," ").trim()
Output:
GO
World
go
, I am Naga
+++++++++++++++++++++++++
GOTO School
GO Heaven
See the Scala demo
The .stripMargin('|') will get rid of the indentation.
Pattern details
(?s) - a DOTALL modifier, . matches any chars including line break chars
& Hello Buddy - a literal string
.*? - any 0+ chars as few as possible up to the first occurrence of...
&endmarker& - a literal string.
If you have a big file to process, the pattern should be re-written for better performance as
"(?s)& Hello Buddy[^&]*(?:&(?!endmarker&)[^&]*)*&endmarker&"
where .*? lazy dot is replaced with an unroll-the-loop pattern [^&]*(?:&(?!endmarker&)[^&]*)* that matches
[^&]* - 0+ chars other than &
(?:&(?!endmarker&)[^&]*)* - zero or more occurrences of:
&(?!endmarker&) - a & not followed with endmarker&
[^&]* - 0+ chars other than &