data_runner
data_runner

Reputation: 73

regex Python: split CamelCase AND STOP IF there is space

All help is greatly appreciated folks I found a brilliant solution here How to do CamelCase split in python ,

However I need it to stop IF THERE is SPACE Example

So, how do I change regex to STOP splitting CamelCase if it finds space?

Upvotes: 1

Views: 267

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626903

You can get the part of the string from its beginning to the first whitespace and apply your solution to that part of the string:

re.sub('([A-Z][a-z]+)', r' \1', re.sub('([A-Z]+)', r' \1', text.split()[0])).split()

See the Python demo, and the following demo below:

import re
l = ['CubsWhite Sox', 'YankeesMets']
for s in l:
    print(f"Processing {s}")
    first = s.split()[0]
    result = re.sub('([A-Z][a-z]+)', r' \1', re.sub('([A-Z]+)', r' \1', first)).split()
    print(result)

Output:

Processing CubsWhite Sox
['Cubs', 'White']
Processing YankeesMets
['Yankees', 'Mets']

Upvotes: 2

Related Questions