正規表現の最後の 2 つの「/」の間のすべてを一致させる必要があります
例: 文字列 tom/jack/sam/jill/ の場合 ---> jill と一致させる必要があります
その場合は tom/jack/sam とも一致する必要があります (最後の '/' なし)
考えていただければ幸いです!
1)
str = "tom/jack/sam/jill/"
*the_rest, last = str.split("/")
the_rest = the_rest.join("/")
puts last, the_rest
--output:--
jill
tom/jack/sam
2)
str = "tom/jack/sam/jill/"
md = str.match %r{
(.*) #Any character 0 or more times(greedy), captured in group 1
/ #followed by a forward slash
([^/]+) #followed by not a forward slash, one or more times, captured in group 2
}x #Ignore whitespace and comments in regex
puts md[2], md[1] if md
--output:--
jill
tom/jack/sam
必要なものが文字列である場合は、とのtom/jack/sam/jill/
2 つのグループを抽出します。必要な正規表現は次のとおりです。jill
tom/jack/sam/
^((?:[^\/]+\/)+)([^\/]+)\/$
regexp は文字列の先頭では受け入れず、文字列の末尾では/
a を要求することに注意してください。/
見てみましょう: http://rubular.com/r/mxBYtC31N2