我如何解析红宝石串的最后一部分?

时间:2012-02-20 19:37:32

标签: ruby-on-rails ruby

我有这个字符串

location = '\\dev-something-again-n2\Staples\Started\477'
location = '\\dev-something-again-n2\Staples\Started\477\'

我需要从中取出477 ...任何有关这样做的好方法的想法......我正在尝试

location.partition("\")

但什么都没有......

3 个答案:

答案 0 :(得分:4)

location.split('\\').last

partition在这里不是正确的工具 - 它会在字符串上拆分一次,而不是像所记录的那样在所有找到的地方拆分:

partition(sep) => [head, sep, tail] click to toggle source
Searches the string for sep and returns the part before it, the sep, and the part after it. If sep is not found, returns str and two empty strings. If no argument is given, Enumerable#partition is called.

split是适合这项工作的工具,如果你想通过打破内容来做到这一点。

答案 1 :(得分:1)

尝试使用:

File.split(location).last

File.basename(location)

答案 2 :(得分:0)

location.chomp('\\').match(/(?:.*\\)(.*)/)[1]