user1030981 user1030981 - 6 months ago 17
Ruby Question

How to get all digits from string regexp ruby

How to get all the digits from a sentence string like "Lorem 123 ipsum 456 879" => "123456879" using regexp in ruby?

Answer

Just replace everything else.

result = subject.gsub(/[^\d]/, '')
Comments