Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Time range help please 08:00am-02:00pm

Last post 05-08-2007, 9:35 AM by akkrdy. 5 replies.
Sort Posts: Previous Next
  •  05-07-2007, 1:08 PM 29521

    Time range help please 08:00am-02:00pm

    Hi I need help constructing the regex expression

    I have created one but it works for only some conditions

    (([1-9]|1[0-2]):[0-5][0-9]am-([1-9]|1[0-2]):[0-5][0-9]pm) this is the one that I created it works for the 08:00am-12:00pm but if I check for 08:00am-02:00am it does not work.

     Could anyone please help. I would really appreciate the help.

    I need it for doing validation in Java

     Thanks in advance.

    --Kiran Kumar

  •  05-07-2007, 2:35 PM 29522 in reply to 29521

    Re: Time range help please 08:00am-02:00pm

    I'm a bit confused here.  What exactly is the format you're trying to validate?  One date that can be between 8:00 am and 2:00 pm, or 2 dates separated by a dash?

  •  05-07-2007, 3:02 PM 29523 in reply to 29522

    Re: Time range help please 08:00am-02:00pm

    it is only one date.

    Thanks for your reply Let me know if you any more details.

    Thanks,

    --Kiran Kumar

  •  05-07-2007, 4:13 PM 29524 in reply to 29523

    Re: Time range help please 08:00am-02:00pm

    Ok, here's how I'd go about it.

     1) The minutes do not vary, they are always 0-59, so [0-5][0-9] describes them completely.

    2) The valid am times are: 08,09,10,11.  The valid pm times are 12, 01, 02

    We can describe the am times with the regex: (0[89]|(1[01])):[0-5][0-9]am

    We can describe the pm times with the regex: (((12|01):[0-5][0-9])|02:00)pm

    So, the final regex that will validate both is the ORed result of the two regex's above:

    ^((0[89]|(1[01])):[0-5][0-9]am)|((((12|01):[0-5][0-9])|02:00)pm)$

    With a ^ and $ to signify beginning and end of string respectively.

    There may be a more efficient way to represent this regex, but this should get you started.

     

     

  •  05-07-2007, 4:15 PM 29525 in reply to 29523

    Re: Time range help please 08:00am-02:00pm

    Ranges, while they can be done, are a really bad application of regex. If you have a range of values and are only selecting one, that's one thing but you are wanting it to understand a range.  You'd be better served using a datetime function to evaluate this.

    A regex of this nature will not guarantee a proper range because regexes don't understand time, just text.  If I put in a time 02:00 PM - 08:00AM it isn't going to know the order is wrong (or going backwards)


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  05-08-2007, 9:35 AM 29543 in reply to 29525

    Re: Time range help please 08:00am-02:00pm

    Thanks for the replies guys.

    I appreciate your help.

    Regards,

    --Kiran Kumar

View as RSS news feed in XML