Commonly you may want to get a substring between to strings or characters in Ruby. I found it a little more difficult to find this information than it should have been.


str = "your +example string@ here"

# Between 1st + and 1st @:
regex = /\+(.*?)@/

# Between 1st + and last @:
regex = /\+(.*)@/

# Between last + and last @:
regex = /.*\+(.*)@/

# Between last + and 1st @:
regex = /.*\+(.*?)@/

# Fetch the value
str[regex, 1]
# or 
str.slice(regex, 1)


Related External Links: