Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if a string is in this format "12:30-14:30" using regex

I’m in a rush so I don’t have time to review regex rules.

I would like to know how do I check if a string is in this format "12:30-14:30", where

  1. the start time has to be before the end time.
  2. the minute section has to be ":00" or ":30".
  3. the hour section is between "08:" and "20:".

Any help will be greatly appreciated! Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The regex answer is:

^((0[8-9]|1[0-9]|20):[03]0)-((0[8-9]|1[0-9]|20):[03]0)$

Note that this does not ensure that the start time is before the end time, you would need additional Java code to do that.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading