Imagine I want to find all time expressions referring to 'AM' and 'PM' in a string. Let's ignore for the moment that I could use '[AP]M' to do this (because I'm actually pulling the list of valid strings ['AM','PM'] from a dictionary whose keys are language codes). I'd like to look for both at once, like this:
foo = ['am','pm']
separator = ':'
timex = re.compile('(1[012]|[1-9])%s([0-5][0-9])( %s)?' % (separator, foo), re.I)
bar = "It's 6:00 pm, do you know where your brain is?"
timex as written above doesn't get me what I'm after: it only matches to the 'p' in 'pm'. (It seems to be treating all the chars of the list elements as though they were [ampm].)
What I don't want is to do two passes over the string (one each for 'am' and 'pm').
Is there a nice Pythonic way to do a single pass for every item in foo?