Split a string into a string and an integer

Use a positive lookbehind assertion based regex in string.split.

> "10480ABCD".split(/(?<=\d)(?=[A-Za-z])/)
=> ["10480", "ABCD"]
  • (?<=\d) Positive lookbehind which asserts that the match must be preceded by a digit character.

  • (?=[A-Za-z]) which asserts that the match must be followed by an alphabet. So the above regex would match the boundary which exists between a digit and an alphabet. Splitting your input according to the matched boundary will give you the desired output.

OR

Use string.scan

> "10480ABCD".scan(/\d+|[A-Za-z]+/)
=> ["10480", "ABCD"]

The splitter is the non-numerical characters themselves:

"10480ABCD".split(/(\D+)/)
# => ["10480", "ABCD"]