'python regix how to split two word and add comma? [closed]

This is my string:

Hair ReplacementHair BraidingHair Supplies & Accessories

my expected result should be:

Hair Replacement,Hair Braiding,Hair Supplies & Accessories

If two word like this ReplacementHair I want to split this two word and add comma between theme.

I tried this code:

re.sub(r"(\w)([A-Z])", r"\1 \2", text)

The above code splitting two word and add space between theme. I want comma instead of space.



Solution 1:[1]

You can replace the space in the replacement pattern with a comma.

import re
text = "Hair ReplacementHair BraidingHair Supplies & Accessories"
text2 = re.sub(r"(\w)([A-Z])", r"\1,\2", text)
print(text2)

output

Hair Replacement,Hair Braiding,Hair Supplies & Accessories

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1