Product code looks like abcd2343, how to split by letters and numbers?

Viewed 81287

I have a list of product codes in a text file, on each line is the product code that looks like:

abcd2343 abw34324 abc3243-23A

So it is letters followed by numbers and other characters.

I want to split on the first occurrence of a number.

6 Answers

This function handles float and negative numbers as well.

def separate_number_chars(s):
    res = re.split('([-+]?\d+\.\d+)|([-+]?\d+)', s.strip())
    res_f = [r.strip() for r in res if r is not None and r.strip() != '']
    return res_f

For example:

utils.separate_number_chars('-12.1grams')
> ['-12.1', 'grams']

Try this code it will work fine

import re
text = "MARIA APARECIDA 99223-2000 / 98450-8026"
parts = re.split(r' (?=\d)',text, 1)
print(parts)

Output:

['MARIA APARECIDA', '99223-2000 / 98450-8026']

Related