Created
May 22, 2012 15:21
-
-
Save ThomasWunderlich/2769724 to your computer and use it in GitHub Desktop.
An explanation of the time regex found in the Saas Book.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
time_regex = /^\d\d?:\d\d\s*[ap]m$/i | |
#^ here means beginning of line | |
#\d is a digit, \s is a (white)space character | |
#the ? means 0 or 1 and affects the character before it. so here it means that | |
#the second digit is optional. Ie if someone wrote 9:25 as opposed to 09:25 | |
#both would be understood. Similarly, the * following \s means 0+ and states | |
#that there could be any amount of spaces | |
#[] is a set, so here [ap] means that the character can be either an a or a p | |
#$ means end of line | |
# the i after the last / means that the whole regex is case-insensitive | |
#Alternate syntax would be: | |
x =~ /(\d\d?):(\d\d)\s*([ap])m$/i | |
#note that =~ returns the position of the matching text | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment