Extract number from string in Ruby

后端 未结 7 2100
既然无缘
既然无缘 2020-12-02 10:59

I\'m using this code:

s = line.match( /ABCD(\\d{4})/ ).values_at( 1 )[0] 

To extract numbers from strings like:

ABCD1234
AB         


        
7条回答
  •  孤城傲影
    2020-12-02 11:48

    your_input = "abc1cd2"
    your_input.split(//).map {|x| x[/\d+/]}.compact.join("").to_i
    

    This should work.

提交回复
热议问题