最后两个分隔符之间的ruby正则表达式匹配字符串
我需要匹配正则表达式中最后两个“/”之间的所有内容 例如:对于字符串tom/jack/sam/jill/-->我需要匹配jill 在这种情况下,还需要匹配tom/jack/sam(不带最后一个“/”)最后两个分隔符之间的ruby正则表达式匹配字符串,ruby,regex,Ruby,Regex,我需要匹配正则表达式中最后两个“/”之间的所有内容 例如:对于字符串tom/jack/sam/jill/-->我需要匹配jill 在这种情况下,还需要匹配tom/jack/sam(不带最后一个“/”) 谢谢 如果您想要的是一个字符串tom/jack/sam/jill/提取两组:jill和tom/jack/sam/。 您需要的regexp是:^((?:[^\/]+\/)+)([^\/]+)\/$ 请注意,regexp不接受字符串开头的/,并在字符串结尾请求/ 看看:1) (二) 有什么特别的原因让
谢谢 如果您想要的是一个字符串
tom/jack/sam/jill/
提取两组:jill
和tom/jack/sam/
。
您需要的regexp是:^((?:[^\/]+\/)+)([^\/]+)\/$
请注意,regexp不接受字符串开头的/
,并在字符串结尾请求/
看看:1)
(二)
有什么特别的原因让你不展开讨论吗?我把你的问题重读了8遍,但我还是不太明白你所说的匹配是什么意思。@Jeffrey:对不起,我说的“匹配”是指我需要提取问题的那一部分string@Chris:我想如果你想要的是:tom/jack/sam/,那么作为一个新的qtn
就不会那么令人困惑了。不。
str = "tom/jack/sam/jill/"
*the_rest, last = str.split("/")
the_rest = the_rest.join("/")
puts last, the_rest
--output:--
jill
tom/jack/sam
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