Regex for find pattern and insert between

I'm in dire need of your regex expertise. I'd like to find a pair of markdown comments and insert some text between them.

Here are the comments:

<!-- {{Start}} -->
<!-- {{End}} -->

which as a string evaluates to

"<!-- {{Start}} -->\n<!-- {{End}} -->".

Here's my desired output after the find and insert:

<!-- {{Start}} -->
Hello World
<!-- {{End}} -->

Any ideas?

Does it have to be regex? Or could you just replace the chunk of string starting with the comment tag ?

# Imports

test_str <- "<!-- {{Start}} -->\n<!-- {{End}} -->"

to_replace <- "Start}} -->\n<"

stringr::str_replace(test_str, coll(to_replace), "Start}} -->\nHello World!<")
#> [1] "<!-- {{Start}} -->\nHello World!<!-- {{End}} -->"

Created on 2019-08-14 by the reprex package (v0.3.0)

PS - from the docs for stringr::str_replace, explains use of coll()

Generally, for matching human text, you'll want coll() which respects character matching rules for the specified locale.

This solved it! Thank you.

Also here's how you could do it with regex

to_replace <- "<!-- {{Start}} -->\n<!-- {{End}} -->"
replacement <-  "Hello World"

                     stringr::regex("Start\\}\\} -->.*\\<",dotall = T),
                     paste0("Start}} -->\\\n",replacement,"<"))

Or with gsub:

test_str <- "<!-- {{Start}} -->\n<!-- {{End}} -->"
gsub("(<!-- \\{\\{Start\\}\\} -->)(\\n)(<!-- \\{\\{End\\}\\} -->)", "\\1\\\nHello World\\\n\\3", test_str)