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

Matching time using re pattern

I am trying to match time using re module

matching is required for the following time format

02:54 pm
02:54pm
02:5407:46pm
02:5407:46 pm
02:5407:46 p.m.
02:5407:46p.m.
2:5407:46p.m.
2:54pm
02:54

I tried using the following pattern

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

\d{1,2}:\d{2}\s?p?\.?m?\.?

But these 02:5407:46 p.m. formats are shown as split format 02:54 and 07:46 p.m.. I want to consider it as a single pattern and no need to consider separately

>Solution :

There might be a more elegant ways to write this regex, but this one should to the trick:

\d{1,2}:\d{2}(?:\d{2}:\d{2})?\s?p?\.?m?\.?
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