I have a program that creates information for a time stamp, the time stamp has to match the following format: MM/DD/YYYY HH:MM:SS AM or PM
For example:
I don't think a regex is the right tool for this job. I'd use DateTime.strptime, give it the format you're expecting and let it deal with all the "50 hours is invalid in a time of day", "09 versus 9", ... issues.
Something like:
require 'date'
def timestamp
print 'Enter the time stamp to verify: '
ts = gets.chomp # You might want to include a String#strip call in here too
DateTime.strptime(ts, '%m/%d/%Y %I:%M:%S %p')
puts ts
rescue ArgumentError
puts 'Invalid formatting of time stamp'
exit 1
end
Regexes are nice but they shouldn't be the only parsing tool you have.