[Rails] 字串中,取得兩字串間的文字
方式一
str = “your +example string@ here”
# Between 1st + and 1st @:
str[/\+(.*?)@/,1]
# Between 1st + and last @:
str[/\+(.*)@/,1]
# Between last + and last @:
str[/.*\+(.*)@/,1]
# Between last + and 1st @:
str[/.*\+(.*?)@/,1]
方式二
i = to.index("+")
j = to.index("@")
to[i+1..j-1]
參考
- http://stackoverflow.com/questions/4218986/ruby-using-regex-to-find-something-in-between-two-strings
留言