我有以下字符串:
this sentence: should be: splited after last colon: sentence
所以我想在最后一次冒号:
之后拆分上面的句子,如下所示:
["this sentence: should be: splited after last colon:", "sentence"]
任何帮助?
答案 0 :(得分:4)
尝试简单的代码:
s = 'this sentence: should be: splited after last colon: sentence'
s =~ /(.*):(.*)?/
[ $1 << ':', $2 ]
# => ["this sentence: should be: splited after last colon:", " sentence"]
答案 1 :(得分:1)
尝试
str = "this sentence: should be: splited after last colon: sentence"
last_pos = str.rindex(/\:/)
arr = [str[0..last_pos].strip, str[last_pos + 1 .. str.length].strip]
=>["this sentence: should be: splited after last colon:", "sentence"]