tags:

views:

60

answers:

1

I'm trying to use Ruby to split to the right of a number.

For example: H2SO4 How do you do this? I'd like to output ["H2", "SO4"]

x.split(/\d+/) yields: ["H", "SO"]

x.split(//) yields: ["H", "2", "S", "O", "4"]

Both cool but not exactly what I'm looking for.

+5  A: 
x.scan(/[A-za-z]*\d+/)

This means break it into groups, each of which contains 0 or more letters, then 1 or more digits. Or if the non-digits can be anything:

x.scan(/\D*\d+/)
Matthew Flaschen