Tom Robbins Tom Robbins - 3 months ago 13
Python Question

Regex ('foo'|'bar') notation

I'm using regex to parse some time data, but my attempt is not matching as I would expect. Here's my code:

import re
print re.findall("\d+:\d+ (am|pm)", "11:30 am - 2:20 pm")


This produces
['am', 'pm']
, not
['11:30 am', '2:20 pm']
, which is what I want.

I can produce the result that I want with
\d+:\d+ am|\d+:\d+ pm
, but that is a little blunt and I want to know why the other is not working?

Answer

Your problem relates to capturing groups. If you want to have non-capturing alternation use the regex \d+:\d+ (?:am|pm).

Comments