[Solved] How to replace a pattern using regular expression?


When working with dates and times, it is almost always best to convert the date first into a Python datetime object rather than trying to attempt to alter it using a regular expression. This can then be converted back into the required date format more easily.

With regards to leading zeros though, the formatting options only give leading zero options, so to get more flexibility it is sometimes necessary to mix the formatting with standard Python formatting:

from datetime import datetime

for test in ['2018-Feb-23-05-18-11', '2018-Feb-23-05-8-11', '2018-Feb-1-0-0-0']:
    dt = datetime.strptime(test, '%Y-%b-%d-%H-%M-%S')
    print '{dt.year}-{}-{dt.day}-{dt.hour}-{dt.minute:02}-{dt.second}'.format(dt.strftime('%b'), dt=dt)

Giving you:

2018-Feb-23-5-18-11
2018-Feb-23-5-08-11
2018-Feb-1-0-00-0

This uses a .format() function to combine the parts. It allows objects to be passed and the formatting is then able to access the object’s attributes directly. The only part that needs to be formatted using strftime() is the month.


This would give the same results:

import re

for test in ['2018-Feb-23-05-18-11', '2018-Feb-23-05-8-11', '2018-Feb-1-0-0-0']:
    print re.sub(r'(\d+-\w+)-(\d+)-(\d+)-(\d+)-(\d+)', lambda x: '{}-{}-{}-{:02}-{}'.format(x.group(1), int(x.group(2)), int(x.group(3)), int(x.group(4)), int(x.group(5))), test)

1

solved How to replace a pattern using regular expression?