Okay. I think I can handle that. The only thing is this: I'm inserting this data into a MySQL database, so, I'll have to change it to 24 hour format anyway. I'll have to decide which I want to do.

I do like that idea of being a bit more relaxed, though it'll make me do more logic so I can insert it into the database, but I'm not too worried about it. One thing that concerns me, though, is how adding that extra parentheses to make the :ss optional will change the order of the data returned. I'm doing this in PHP, and sometimes it seems a bit quirky in its backreference ordering, especially with nested parentheses. I suppose I could deliberately make them non-capturing parentheses like this:

/^(0?[1-9]|1[0-2]):([0-5][0-9])(?::([0-5][0-9]))?\s*(AM|PM|am|pm)$/

Would that work?

The more I think about it, the more I like the idea of changing it to 24 hour format first anyway, but then I need to verify the format first, and extract the hours and AM|PM before I can change it from 12 to 24.

Ah, well. Thanks for the help.

Lloyd


P.S. What's the difference between \s and \W? \w refers to any word character, and so it seems everything else (i.e. \W) is whitespace. So then what is \s? I'm so confused.... :-P




Jacob Fugal wrote:

QuickBrownFox wrote:

Hey everybody,

I'm using a regular expression for validation here at work that I wanted some advice on. Specifically, it checks to see if the time is formatted correctly in the 12-hour format (hh:mm:ss (AM|PM)). So far, this is what I've got:

/^(0?[1-9]|1[0-2]):([0-5][0-9]):9[0-5][0-9]) (AM|PM)$/

^
Looks fine, except I think you missed a shift key (above the marker, if that 9 is a (, then everything's fine). Although I'd relax the requirement for seconds and allow variable whitespace:


/^(0?[1-9]|1[0-2]):([0-5][0-9])(:([0-5][0-9]))?\s*(AM|PM)$/

Now, my question is this: How would I change this to only allow times from 8:00 AM to 5:00 PM.


Rather than trying a regex, I'd use logic in the language after the regex. For example, if I were in perl:

sub check_time
{
   # get input and check general format

   my $time = shift;
   if ($time =~
       /^(0?[1-9]|1[0-2]):([0-5][0-9])(:([0-5][0-9]))?\s*(AM|PM)$/)
   {
      # get parts and convert to 24 hours string w/o colons (default
      # seconds to 0

      my ($hour, $min, $sec, $am_or_pm) = ($1, $2, $4, $5);
      $hour += 12 if $am_or_pm eq 'PM';
      $sec = 0 unless $sec;
      my $new_time = $hour . $min . $sec;

# make sure the time is between 8AM and 5PM (inclusive)

      return ($new_time ge '080000' and $new_time le '170000');
   }
   else { return 0; }
}

Jacob Fugal


____________________
BYU Unix Users Group http://uug.byu.edu/ ___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list




--


--------------------
"Open Source is not a crime."
               -Seen on a bumper sticker
--------------------



____________________
BYU Unix Users Group http://uug.byu.edu/ ___________________________________________________________________
List Info: http://uug.byu.edu/cgi-bin/mailman/listinfo/uug-list

Reply via email to