Regex to extract two dates out of a date range
<p>I have a date range and I want to extract the two dates, this is an example string:</p>

<pre><code>Sep 25-28, 2016
</code></pre>

<p>and I'd like to have two regular expressions, one that matches:</p>

<pre><code>Sep 25, 2016
</code></pre>

<p>and the other that matches:</p>

<pre><code>Sep 28, 2016
</code></pre>

<p>But then I'd like that also matches:</p>

<pre><code>Sep 29-Oct 2, 2016
</code></pre>

<p>This is what I have built so far:</p>

<pre><code>(?P&lt;date&gt;\b(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|[0-9]|1[0-2]) (\d|[0-2][0-9]|3[0-1])(\s|\.|-)(:?\d|[0-2][0-9]|3[0-1]),?(\s|\.|-)\b\d{1,4}\b)
</code></pre>

<p>But of course matches the whole range.</p>

<p>Any help?</p>

<p>Looking at your sample ranges, it looks like they follow this pattern:</p>

<p><code>BEGIN_MONTH</code> <code>SPACE</code> <code>BEGIN_DAY</code> <code>DASH</code> <code>END_MONTH (optional)</code> <code>END_DAY</code> <code>COMMA</code> <code>SPACE</code> <code>YEAR</code></p>

<p>From that, you want to generate two strings:</p>

<p><code>BEGIN_MONTH</code> <code>SPACE</code> <code>BEGIN_DAY</code> <code>COMMA</code> <code>SPACE</code> <code>YEAR</code></p>

<p><code>END_MONTH (if present; otherwise use BEGIN_MONTH)</code> <code>SPACE</code> <code>END_DAY</code> <code>COMMA</code> <code>SPACE</code> <code>YEAR</code></p>

<p>Is this correct?  Do you need to account for beginning and ending year, if a date range spans across a year boundary?</p>

<pre><code>import re

pattern = '(\w+) (\d+)-(\w+ )?(\d+), (\d+)'
pc = re.compile(pattern)

text = 'Sep 25-Oct 5, 2016'
# text = 'Sep 25-29, 2016' -- also works in this format

if pc.match(text).group(3):
    # second month name is present
    print ('%s %s-%s%s, %s' % (pc.match(text).group(1),
                                pc.match(text).group(2),
                                pc.match(text).group(3),
                                pc.match(text).group(4),
                                pc.match(text).group(5)))

else:
    print ('%s %s-%s %s, %s' % (pc.match(text).group(1),
                                pc.match(text).group(2),
                                pc.match(text).group(1),
                                pc.match(text).group(4),
                                pc.match(text).group(5)))
</code></pre>

python regex