Search code examples
pythondatesubstringextract

Python Substring Regex Extraction


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?


Solution

  • 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