Home > OS >  How to convert YYYY-MM-DD to MM/DD/YYYY by using regular expression?
How to convert YYYY-MM-DD to MM/DD/YYYY by using regular expression?

Time:12-19

As mentioned above, I need to convert to mm/dd/yyyy and also remove those which is not related.

text = '''
1.   abc  2016-10-31  
2.   xyz   2016-9-4
3.   aef   2016-10-aa
4.    asasf asdf 10-14
5.  2013-10-3  234234
'''

Output

1. 10/31/2016
2. 09/04/2016
3. 10/03/2013

I greatly appreciate your help!

CodePudding user response:

If you are insisting on using regex, you can try code below:

import re
import datetime
text = '''
1.   abc  2016-10-31  
2.   xyz   2016-9-4
3.   aef   2016-10-aa
4.    asasf asdf 10-14
5.  2013-10-3  234234
'''
regex = re.findall("\d{4}\-\d{1,2}-\d{1,2}", text)
def mapFunc(value):
  date = datetime.datetime.strptime(value, '%Y-%m-%d')
  return date.strftime('%m/%d/%Y')
result = list(map(mapFunc, regex))
print(*result, sep='\n')

Explanation

The regex that is used in this code is \d{4}\-\d{1,2}-\d{1,2} which filters the date given in the text varaible. Then using datetime module, you can reformat the filtered date to whatever you like.

Output

10/31/2016
09/04/2016
10/03/2013

CodePudding user response:

If you need to extract the dates, then use re.finditer with capture groups and reformat each match:

import re
for s in re.finditer(r"\b(\d{4})-(\d\d?)-(\d\d?)\b", text):
    print(f"{s.group(2):0>2}/{s.group(3):0>2}/{s.group(1)}")

If you need to correct the text, then use re.sub:

import re
text = re.sub(r"\b(\d)/", r"0\1/",  
    re.sub(r"\b(\d{4})-(\d\d?)-(\d\d?)\b", r"\2/\3/\1", text)
)

CodePudding user response:

You could do it like this without the need for RE:

text = '''
1.   abc  2016-10-31  
2.   xyz   2016-9-4
3.   aef   2016-10-aa
4.    asasf asdf 10-14
5.  2013-10-3  234234
'''
from datetime import datetime
for token in text.split():
    try:
        d = datetime.strptime(token, '%Y-%m-%d')
        print(datetime.strftime(d, '%m/%d/%Y'))
    except ValueError:
        pass

Output:

10/31/2016
09/04/2016
10/03/2013
  • Related