Python Regex matching any order
<p>Lets say I have datetime in the format</p>

<pre><code>12 September, 2016
September 12, 2016
2016 September, 12
</code></pre>

<p>I need regex like it should return match in same order always for any dateformat given above</p>

<pre><code>match-1 : 12
match-2 : September
match-3 : 2016
</code></pre>

<p>I need results in the same order always. </p>

<p>Named groups as suggested below is a good way of doing it (especially if you already have the regexes set up) but for completion's sake here's how to handle it with the <code>datetime</code> module.</p>

<pre><code>from datetime import datetime as date

def parse_date(s):
    formats = ["%d %B, %Y",
               "%B %d, %Y",
               "%Y %B, %d"]

    for f in formats:
        try:
            return date.strptime(s, f)
        except ValueError:
            pass

    raise ValueError("Invalid date format!")

arr = ["12 September, 2016",
       "September 12, 2016",
       "2016 September, 12",
       "12/9/2016"]

for s in arr:
    dt = parse_date(s)      
    print(dt.year, dt.strftime("%B"), dt.day)

"""

2016 September 12
2016 September 12
2016 September 12
Traceback (most recent call last):
  File "C:/Python33/datetest.py", line 22, in &lt;module&gt;
    dt = parse_date(s)
  File "C:/Python33/datetest.py", line 19, in parse_date
    raise ValueError("Invalid date format!")
ValueError: Invalid date format!

"""
</code></pre>

<p>For more information, see the <a href="https://docs.python.org/3/library/datetime.html" rel="nofollow">datetime documentation page</a>.</p>

python regex datetime match