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>You can't switch the group order but you can name your groups:</p>

<pre><code>(r'(?P&lt;day&gt;[\d]{2})(?:\s|,|\?|$)|(?P&lt;month&gt;[a-zA-Z]+)|(?P&lt;year&gt;[\d]{4})')
</code></pre>

<ul>
<li><p><code>(?P&lt;day&gt;[\d]{2})(?:\s|,|\?|$)</code>: matches a day, can be accessed in python with <code>l.group("day")</code></p></li>
<li><p><code>(?P&lt;month&gt;[a-zA-Z]+)</code>: matches a month, can be accessed in python with <code>l.group("month")</code></p></li>
<li><p><code>(?P&lt;year&gt;[\d]{4})</code>: matches a year, can be accessed in python with <code>l.group("year")</code></p></li>
</ul>

<p>Example:</p>

<pre><code>import re

data = """
12 September, 2016
September 12, 2016
2016 September, 12
September 17, 2012
17 October, 2015
"""

rgx = re.compile(r'(?P&lt;day&gt;[\d]{2})(?:\s|,|\?|$)|(?P&lt;month&gt;[a-zA-Z]+)|(?P&lt;year&gt;[\d]{4})')

day = ""
month = ""
year = ""

for l in rgx.finditer(data):
        if(l.group("day")):
                day = l.group("day")
        elif(l.group("month")):
                month = l.group("month")
        elif(l.group("year")):
                year = l.group("year")

        if(day != "" and month != "" and year != ""):
                print "{0} {1} {2}".format(day, month, year)
                day = ""
                month = ""
                year = ""
</code></pre>

<p><a href="https://ideone.com/4o0vfQ" rel="nofollow">Demo</a></p>

python regex datetime match