Regex doesn't filter out the right text on datatime
<p>I have a string below:</p>

<pre><code>senton = "Sent:                               Friday, June 18, 2010 12:57 PM"
</code></pre>

<p>I created a regex to filter out the datetime portion:</p>

<pre><code>reg_datetime = "(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), (January|February|March|April|May|June|July|August|September|October|November|December) \d{1,2}, \d{4} \d{2}:\d{2} (AM|PM)"
</code></pre>

<p>I tested the regex in regex101.com and it works as expected, however, when running it in my python test script, it fails to give me the right text, can anyone help me fix it?</p>

<p>Using it this way:</p>

<pre><code>real_senton = re.findall(reg_datetime, senton)
print real_senton
</code></pre>

<p>Produces this result (<a href="https://i.stack.imgur.com/Si8U8.jpg" rel="nofollow">here is the screenshot</a>):</p>

<pre><code>[('Friday', 'June', 'PM')]
</code></pre>

<p>Thank you very much.</p>

<p>Function <a href="https://docs.python.org/2/library/re.html#re.findall" rel="nofollow"><code>re.findall</code></a> does the following:</p>

<blockquote>
  <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. 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. Empty matches are included in the result unless they touch the beginning of another match.</p>
</blockquote>

<p>So, if there are groups, it returns the groups. A <em>group</em> is anything in the regular expression enclosed in parenthesis.</p>

<h2>solution 1</h2>

<p>To get every item separately, put everything into parentesis:</p>

<pre><code>reg_datetime = "(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), "\
 "(January|February|March|April|May|June|July|August|September|October|November|December)"\
 " (\d{1,2}), (\d{4}) (\d{2}):(\d{2}) (AM|PM)"
</code></pre>

<p>Then will <code>re.findall(reg_datetime, senton)</code> return:</p>

<pre><code>[('Friday', 'June', '18', '2010', '12', '57', 'PM')]
</code></pre>

<h2>solution 2</h2>

<p>Alternatively, put everything into one big group:</p>

<pre><code>reg_datetime = "((Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), "\
 "(January|February|March|April|May|June|July|August|September|October|November|December)"\
 " \d{1,2}, \d{4} \d{2}:\d{2} (AM|PM))"
</code></pre>

<p>Now the big group is returned as well:</p>

<pre><code>[('Friday, June 18, 2010 12:57 PM', 'Friday', 'June', 'PM')]
</code></pre>

<h2>solution 3</h2>

<p>Or change the existing grops into non-capturing groups (syntax <code>(?:...)</code>)</p>

<pre><code>reg_datetime = "(?:Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday), "\
 "(?:January|February|March|April|May|June|July|August|September|October|November|December)"\
 " \d{1,2}, \d{4} \d{2}:\d{2} (?:AM|PM)"
</code></pre>

<p>Result:</p>

<pre><code>['Friday, June 18, 2010 12:57 PM']
</code></pre>

<h2>solution 4</h2>

<p>Or don't use <code>findall</code> at all. Use <a href="https://docs.python.org/2/library/re.html#re.search" rel="nofollow"><code>re.search</code></a>. It returns a <a href="https://docs.python.org/2/library/re.html#match-objects" rel="nofollow"><code>Match</code></a> object, which gives you more options. With the original <code>reg_datetime</code> it works this way:</p>

<pre><code>&gt;&gt;&gt; m = re.search(reg_datetime, senton)
&gt;&gt;&gt; m.group(0)
'Friday, June 18, 2010 12:57 PM'
&gt;&gt;&gt; m.group(1)
'Friday'
&gt;&gt;&gt; m.group(2)
'June'
&gt;&gt;&gt; m.group(3)
'PM'
</code></pre>

python regex