-2

我需要匹配正则表达式中最后两个“/”之间的所有内容

例如:对于字符串 tom/jack/sam/jill/ ---> 我需要匹配 jill

并且在这种情况下还需要匹配 tom/jack/sam(没有最后一个'/')

想法赞赏!

4

2 回答 2

0

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
于 2013-08-04T00:53:11.317 回答
0

如果你想要的是一个字符串,则tom/jack/sam/jill/提取两组:jilltom/jack/sam/. 您需要的正则表达式是:^((?:[^\/]+\/)+)([^\/]+)\/$.

请注意,正则表达式不接受/以字符串开头并在字符串/末尾请求 a。

看看:http ://rubular.com/r/mxBYtC31N2

于 2013-08-03T18:50:08.113 回答