Fetching the substring contained between two specific words

Simple regular expression would do:

@var = "Hi, I want to extract container_start **ONLY THIS DYNAMIC CONTENT** container_end from the message contained between the container_start and container_end "
@var[/container_start(.*?)container_end/, 1] # => " **ONLY THIS DYNAMIC CONTENT** "

Using the same regex given by Victor, you can also do

var.split(/container_start(.*?)container_end/)[1]

Just to provide a non-regex answer, you can also use two .splits with selection of array entries.

=> @var = "Hi, I want to extract container_start ONLY THIS DYNAMIC CONTENT container_end from the message contained between the container_start and container_end "
=> @var.split("container_start ")[1].split(" container_end")[0]
=> "ONLY THIS DYNAMIC CONTENT"

.split splits the string at the text in the quotes. The [1] selects the portion AFTER that text. For the second cut, you want the portion BEFORE the "container_end" so you select [0].

You need to leave the spaces in the two .split substrings to remove leading and trailing spaces. Alternately, use .lstrip and .rstrip.

If there were more "container_start" and "container_end" strings you would need to adjust the array selectors to pick the correct portion of @var between those two substrings.