I have a passage of text that contains VALID dates formatted as dd/mm/yyyy. I need to replace all months to their respective month name.
10/05/1999 -> 10 May 1999
I find dates and capture each part in its own group with
'([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})'
I need to pass group 2 to a function/dictionary that returns month name. Unsure of how to do that.
def test(s):
print(s)
return f">> {s} <<"
rep = re.sub('([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{2,4})', '\g<1> '+test('\g<2>')+' \g<3>', '10/05/1999')
#execution of test prints below
#'\g<2>'
print(rep)
#10 >> 05 << 1999
As you can see the literal string '\g<2>'
is passed to test() function rather than the group content itself, which is '05'
. As a result I cannot perform month number to month name conversion. What can be done instead?