Home > Blockchain >  Matching time using re pattern
Matching time using re pattern

Time:11-30

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

\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

CodePudding user response:

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?\.?

CodePudding user response:

Seems you are cleaning up some dirty data. However, I just cooked up an extended version of your regex, which matches all the lines you mentioned. Its like below.

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

See here

  • Related