0

Im trying to use regex(python) to match the time in a string while accounting for user discrepancies such as am pm or capitols, a single number to start(9:30) and use of a . or : . I am very close with the following:

(\d{1,2}(:|.)\d{2}\s?(?:AM|PM|am|pm))

but when using https://pythex.org/ to check it returns two matches for most strings, example string:

@Stand-up Bot at 4.30am every day

The issue is that I'm getting two matches, one for the time 4.30am which is great but I'm also getting a second match for the separating character in between the numbers . or :.

How can I update the regex to return just the exact time match and not the separating character too?

Thanks

Wazza
  • 1,222
  • 8
  • 35
  • 1
    `(:|.)` => `(?::|\.)` => `[:.]`. The whole => `(?i)\d{1,2}[:.]\d{2}\s?[ap]m` – Wiktor Stribiżew Apr 12 '19 at 23:00
  • thank you, was losing my mind – Wazza Apr 12 '19 at 23:02
  • 1
    You are actually getting 1 match. The issue is that you are capturing 2 groups, due to the 2 sets of capturing parentheses `()`: one set surrounds the whole regex, and the second surrounds the `:|.`. You should make the second one non-capturing using `(?:)` like you did with the AM-PM stuff, like this: `(?::|.)`. Also, `.` is a special character in a regex that matches anything (e.g. `4X30am`), so you should escape it like this: `:|\.`. – almiki Apr 12 '19 at 23:05

0 Answers0