Home > Mobile >  Split a string at uppercase characters and numbers ignoring consecutive uppercase letters in Python
Split a string at uppercase characters and numbers ignoring consecutive uppercase letters in Python

Time:08-30

I am trying to split a string in python based on uppercase characters and numbers ignoring consecutive uppercase letters.

For example, ThisIs9ATestStringABC95 should look like This Is 9 A Test String ABC 95.

I tried to do a search with regular expression using the formula

val = re.findall(r'[A-Z](?:[A-Z]*(?![a-z])|[a-z]*)', `ThisIs9ATestStringABC95` )

but it is ignoring the numbers

CodePudding user response:

You aren't actually looking for digits in your expression. So you could specify like this or like Johhny Mopp mentions with \d

val = re.findall(r'[0-9] |[A-Z](?:[A-Z]*(?![a-z])|[a-z]*)', 'ThisIs9ATestStringABC95' )
  • Related