Extracting numbers from text with Ruby

Asked 2 years ago, Updated 2 years ago, 28 views

Is there a short way to extract a number like 1 or 10 from a string like Level 1 or Level 10? I think Ruby made something besides using regular expressions.

ruby

2022-09-22 18:40

1 Answers

I don't think there's a function that's made separately. I used gsub to exclude non-numeric characters as regular expressions.

print 'level 1'.gsub(/[^0-9]/,'')


2022-09-22 18:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.