Python Substring Regex Extraction

Question:

I have the following types of strings:
FWI20010112
DC20030405 etc…

I need to extract segments of the strings into separate variables like this (example):

name: FWI

year: 2001

month: 01

day: 12

So, the name segment of the string can vary between 2 or 3 characters, and the following digits always follow the same format yyyymmdd. How can I write a regex that extracts this information?

Asked By: tbmsilva

||

Answers:

import re

strings_list = ['FWI20010112','DC20030405']
print(re.sub(r'(w*)(d{4})(d{2})(d{2})',r'name: 1, year: 2, month: 3, day: 4','n'.join(strings_list)))

Output:

name: FWI, year: 2001, month: 01, day: 12
name: DC, year: 2003, month: 04, day: 05
Answered By: Suneesh Jacob
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.