Skip to content

Instantly share code, notes, and snippets.

@ThomasWunderlich
Created May 22, 2012 15:21
Show Gist options
  • Save ThomasWunderlich/2769724 to your computer and use it in GitHub Desktop.
Save ThomasWunderlich/2769724 to your computer and use it in GitHub Desktop.
An explanation of the time regex found in the Saas Book.
#!/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