最后一次出现字符时分割字符串

tok*_*khi 5 ruby regex split ruby-on-rails

我有下面的字符串:

this sentence: should be: split after last colon: sentence

我想在最后一个冒号(:)上拆分上述字符串,以使结果数组包含以下两个元素:

["this sentence: should be: splited after last colon:", "sentence"]
Run Code Online (Sandbox Code Playgroud)

Мал*_*евъ 4

尝试简单的代码:

s = 'this sentence: should be: splited after last colon: sentence'
s =~ /(.*):(.*)?/
[ $1 << ':', $2 ]
# => ["this sentence: should be: splited after last colon:", " sentence"]
Run Code Online (Sandbox Code Playgroud)