I have a string: HotelCityClass. I want to add a space in-between each Uppercase letter (apart from the first). i.e. Hotel City Class.
I have tried using re
re.sub(r'[A-Z]', '', str_name)
But this only replaces each uppercase. Is re the correct, fast approach?
If you have to deal with CaMeL words, you can use the following regex:
([a-z])([A-Z])
It captures a lowercase letter and the following uppercase one and then in the replacement, we can add the back-references to the captured groups (\1 and \2).
import re
p = re.compile(r'([a-z])([A-Z])')
test_str = "HotelCityClass"
result = re.sub(p, r"\1 \2", test_str)
print(result)
See IDEONE demo
Note that in case you want to just insert a space before any capitalized word that is not preceded with a whitespace, I'd use
p = re.compile(r'(\S)([A-Z])')
result = re.sub(p, r"\1 \2", test_str)
See another IDEONE demo
I would not use any look-aheads here since they are always hampering performance (although in this case, the impact is too small).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With