Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Given this string
"In 2015, I want to know how much does iPhone 6+ cost?"
how can I return [2015, 6]?
[2015, 6]
My first attempt at this was
numbers = str.tr("^0-9", '')
but that produces 20156.
20156
tr
You can use scan:
scan
numbers = str.scan(/\d+/) # => ["2015", "6"]
Add a comment
.map(&:to_i)
/\-?\d+/
scan is better, but this also works.
"In 2015, I want to know how much does iPhone 6+ cost?".gsub(/\D/,' ').split #=> ["2015", "6"]
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
tr, then you need to do more.