pythonregex

How to split a code looking like '10abc' in numbers then letters?


I would like to split (in python) 10abc (or codes that start with any number of numbers followed by any number of letters) into 10 and abc (one string with the numbers and one string with the letters).

I have found this question, but it adds an empty string at the start of the resulting list: Product code looks like abcd2343, how to split by letters and numbers?

import re
s="10abc"
re.split('(\d+)',s)
Out[13]: ['', '10', 'abc']

Please, what would be the appropriate regex pattern?

Thanks for your help!


Solution

  • You can use lookaround assertions to split at the zero-width gap that is preceded by a number and followed by a letter:

    s = "10abc"
    print(re.split(r'(?<=\d)(?=[a-z])', s)) # ['10', 'abc']
    

    Demo here