s = "-a Apple -b Ball -c Chocolate"
One way: calculate an index
marker = "-c"
s[s.index(marker)+marker.size+1..-1]
#=> "Chocolate"
marker = "-b"
s[s.index(marker)+marker.size+1..-1]
#=> "Ball -c Chocolate"
marker = "-a"
s[s.index(marker)+marker.size+1..-1]
#=> "Apple -b Ball -c Chocolate"
Another way: use a regex
`\K` in the regex below means "forget everything matched so far".
marker = "-c"
s[/#{marker}\s+\K.*/]
#=> "Chocolate"
marker = "-b"
s[/#{marker}\s+\K.*/]
#=> "Ball -c Chocolate"
marker = "-a"
s[/#{marker}\s+\K.*/]
#=> "Apple -b Ball -c Chocolate"
Consider the regex for one of these markers.
marker = "-a"
r = /
#{marker} # match the contents of the variable 'marker'
\s+ # match > 0 whitespace chars
\K # forget everything matched so far
.* # match the rest of the line
/x # free-spacing regex definition mode
#=> /
# -a # match the contents of the variable 'marker'
# \s+ # match > 0 whitespace chars
# \K # forget everything matched so far
# .* # match the rest of the line
# /x
s[r]
#=> "Apple -b Ball -c Chocolate"
But if you really want just the text between markers
I will construct a hash with markers as keys and text as values. First, we will use the following regex to split the string.
r = /
\s* # match >= 0 spaces
\- # match hypen
( # begin capture group 1
[a-z] # match marker
) # end capture group 1
\s* # match >= 0 spaces
/x # free-spacing regex definition mode
h = s.split(r).drop(1).each_slice(2).to_h
#=> {"a"=>"Apple", "b"=>"Ball", "c"=>"Chocolate"}
With this hash we can retrieve the text for each marker.
h["a"]
#=> "Apple"
h["b"]
#=> "Ball"
h["c"]
#=> "Chocolate"
The steps to create the hash are as follows.
a = s.split(r)
#=> ["", "a", "Apple", "b", "Ball", "c", "Chocolate"]
Notice that, by putting [a-z] within a capture group in the regex, "a", "b" and "c" are included in the array a. (See String#split, third paragraph.)
b = a.drop(1)
#=> ["a", "Apple", "b", "Ball", "c", "Chocolate"]
c = b.each_slice(2)
#=> #<Enumerator: ["a", "Apple", "b", "Ball", "c", "Chocolate"]:each_slice(2)>
We can see the elements of the enumerator c by converting it to an array:
c.to_a
#=> [["a", "Apple"], ["b", "Ball"], ["c", "Chocolate"]]
Lastly,
c.to_h
#=> {"a"=>"Apple", "b"=>"Ball", "c"=>"Chocolate"}
"-a Apple -c Chocolate -b Ball", you want" Chocolate -b Ball"(everything after-c), right?-c.