Home > front end >  Split string from digits/number according to sentence length
Split string from digits/number according to sentence length

Time:01-19

I have cases that I need to seperate chars/words from digits/numbers which are written consecutively, but I need to do this only when char/word length more than 3.

For example, input

ferrari03

output must be:

ferrari 03

However, it shouldn't do any action for the followings: fe03, 03fe, 03ferrari etc.

Can you help me on this one ? I'm trying to do this without coding any logic, but re lib in python.

CodePudding user response:

Using re.sub() we can try:

inp = ["ferrari03", "fe03", "03ferrari", "03fe"]
output = [re.sub(r'^([A-Za-z]{3,})([0-9] )$', r'\1 \2', i) for i in inp]
print(output)  # ['ferrari 03', 'fe03', '03ferrari', '03fe']

Given an input word, the above regex will match should that word begin with 3 or more letters and end in 1 or more digits. In that case, we capture the letters and numbers in the \1 and \2 capture groups, respectively. We replace by inserting a separating space.

  • Related