Home > Back-end >  Python - Split string on last alpha character
Python - Split string on last alpha character

Time:05-05

I have a list of strings that looks like this: ['20D0001', '10A4A001', 'B1999'].

I'd like to split the strings on the last alpha character in the string so that they look like this: [['20D', '0001'], ['10A4A', '001'], ['B', '1999']].

My Python isn't the best so any help is appreciated!

CodePudding user response:

import re

old_list = ["20D0001", "10A4A001", "B1999"]

matches = [re.findall("(.*[A-Z])(.*)", x, flags=re.IGNORECASE)[0] for x in old_list]
list_matches = [list(x) for x in matches]

> [['20D', '0001'], ['10A4A', '001'], ['B', '1999']]

A quick solution is to use regex with capture groups :)

  • Related