Regex ('foo'|'bar') notation
<p>I'm using regex to parse some time data, but my attempt is not matching as I would expect.  Here's my code:</p>

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

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

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

<p><a href="https://docs.python.org/2/library/re.html#re.findall" rel="nofollow">Quoting docs</a> (<strong>emphasis mine</strong>):</p>

<blockquote>
  <p><code>re.findall(pattern, string, flags=0)</code></p>
  
  <p>Return all non-overlapping matches of pattern in string, as a list of
  strings. The string is scanned left-to-right, and matches are returned
  in the order found. <strong>If one or more groups are present in the pattern,
  return a list of groups; this will be a list of tuples if the pattern
  has more than one group.</strong> Empty matches are included in the result
  unless they touch the beginning of another match.</p>
</blockquote>

<p>You may use <code>re.finditer</code>:</p>

<pre><code>seq = [m.string[m.start():m.end()] for m in re.finditer("\d+:\d+ (am|pm)", "11:30 am - 2:20 pm")]
# ['11:30 am', '2:20 pm']
</code></pre>

python regex