[英]Ruby: Split string at character, counting from the right side
Short version -- How do I do Python rsplit() in ruby? 简短版 - 如何在ruby中使用Python rsplit()?
Longer version -- If I want to split a string into two parts (name, suffix) at the first '.' 更长的版本 - 如果我想在第一个'。'将一个字符串拆分为两个部分(名称,后缀)。 character, this does the job nicely:
性格,这很好地完成了工作:
name, suffix = name.split('.', 2)
But if I want to split at the last (rightmost) '.' 但如果我想在最后 (最右边)分开'。' character, I haven't been able to come up with anything more elegant than this:
性格,我还没有能够提出比这更优雅的东西:
idx = name.rindex('.')
name, suffix = name[0..idx-1], name[idx+1..-1] if idx
Note that the original name string may not have a dot at all, in which case name should be untouched and suffix should be nil; 请注意,原始名称字符串可能根本没有点,在这种情况下,名称应该是不变的,后缀应该是nil; it may also have more than one dot, in which case only the bit after the final one should be the suffix.
它也可能有一个以上的点,在这种情况下,只有最后一个后面的位应该是后缀。
String#rpartition
does just that: String#rpartition
就是这样做的:
name, match, suffix = name.rpartition('.')
It was introduced in Ruby 1.8.7, so if running an earlier version you can use require 'backports/1.8.7/string/rpartition'
for that to work. 它是在Ruby 1.8.7中引入的,所以如果运行早期版本,你可以使用
require 'backports/1.8.7/string/rpartition'
来实现它。
Put on the thinking cap for a while and came up with this regexp: 戴上思考上限一段时间,想出了这个正则表达式:
"what.to.do.now".split(/\.([^.]*)$/)
=> ["what.to.do", "now"]
Or in human terms "split at dot, not followed by another dot, at end of string". 或者用人的术语“在点上分开,而不是在字符串的末尾跟着另一个点”。 Works nicely also with dotless strings and sequences of dots:
与无点字符串和点序列一起使用也很好:
"whattodonow".split(/\.([^.]*)$/)
=> ["whattodonow"]
"what.to.do...now".split(/\.([^.]*)$/)
=> ["what.to.do..", "now"]
Here's what I'd actually do: 这就是我实际做的事情:
/(.*)\.(.*)/.match("what.to.do")[1..2]
=> ["what.to", "do"]
or perhaps more conventionally, 或者更传统的,
s = "what.to.do"
s.match(/(.*)\.(.*)/)[1..2]
=> ["what.to", "do"]
if this="what.to.do" you can do this: 如果这=“what.to.do”你可以这样做:
this.split(%r{(.+)\.})
and you'll get back 你会回来的
["", "what.to", "do"]
If you want the literal version of rsplit
, you can do this (this is partly a joke, but actually works well): 如果你想要
rsplit
的文字版本,你可以这样做(这部分是一个笑话,但实际上效果很好):
"what.to.do".reverse.split('.', 2).map(&:reverse).reverse
=> ["what.to", "do"]
You can also add rsplit
to the String class by adding this at the top of your file. 您还可以通过在文件顶部添加
rsplit
将其添加到String类中。
class String
def rsplit(pattern=nil, limit=nil)
array = self.split(pattern)
left = array[0...-limit].join(pattern)
right_spits = array[-limit..]
return [left] + right_spits
end
end
It doesn't quite work like split. 它不像拆分那样工作。 eg
例如
$ pry
[1] pry(main)> s = "test.test.test"
=> "test.test.test"
[2] pry(main)> s.split('.', 1)
=> ["test.test.test"]
[3] pry(main)> s.split('.', 2)
=> ["test", "test.test"]
[4] pry(main)> s.split('.', 3)
=> ["test", "test", "test"]
vs. 与
[6] pry(main)> s
=> "test.test.test"
[7] pry(main)> s.rsplit('.', 1)
=> ["test.test", "test"]
[8] pry(main)> s.rsplit('.', 2)
=> ["test", "test", "test"]
But I can't quite work out how to short split it. 但我无法弄清楚如何缩短它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.