If you really don't need spaces in delimiters, and you want to have a consistent handle before and after, use this:
test = [ "4", "Section 2", "4 Section", "Section 5 Aisle", ] require 'pp' pp test.map{ |str| str.split(/\s*(\d+)\s*/,-1) } #=> [["", "4", ""], #=> ["Section", "2", ""], #=> ["", "4", "Section"], #=> ["Section", "5", "Aisle"]]
Thus, you can always:
prefix, digits, suffix = str.split(/\s*(\d+)\s*/,-1) if prefix.empty? ... end
... instead of checking the length of your matches or some.
Phrogz
source share