Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format string by adding a space before each uppercase letter

Tags:

python

regex

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?

like image 490
Prometheus Avatar asked Dec 06 '25 07:12

Prometheus


1 Answers

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).

like image 95
Wiktor Stribiżew Avatar answered Dec 08 '25 22:12

Wiktor Stribiżew