I'm trying to prompt the user to input a data value, and check that the string they enter only contains the digits 0-9:
class String
def validate(regex)
!self[regex].nil?
end
end
regex = /\A[0-9]\z/
validInput = false
until validInput
puts "Enter digits: "
input = gets.chomp
if !input.validate(regex)
puts "Invalid input. Seeking integer."
else
validInput = true
end
end
However this loop returns false on everything other than single digits excluding 0. I previously had this working with regex = /\A[1-9]\z/, but I going back to where it was working, it doesn't anymore, and this also means the user can't enter 0.
How can I use a regex to validate that user input only contains the digits 0-9?