Extract number from string in Ruby

后端 未结 7 2067
既然无缘
既然无缘 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:49

    To extract number part from a string use the following:

    str = 'abcd1234'
    /\d+/.match(str).try(:[], 0)
    

    It should return 1234

    0 讨论(0)
提交回复
热议问题