我需要匹配正则表达式中最后两个“/”之间的所有内容
例如:对于字符串 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/
提取两组:jill
和tom/jack/sam/
. 您需要的正则表达式是:^((?:[^\/]+\/)+)([^\/]+)\/$
.
请注意,正则表达式不接受/
以字符串开头并在字符串/
末尾请求 a。