开发者

How to split to the right of a number?

开发者 https://www.devze.com 2023-01-08 02:49 出处:网络
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\"]

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开发者_StackOverflow中文版"]

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


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+/)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号