Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split string with commas while keeping numeric parts

I’m using the following function to separate strings with commas right on the capitals, as long as it is not preceded by a blank space.

def func(x):

y = re.findall('[A-Z][^A-Z\s]+(?:\s+\S[^A-Z\s]*)*', x)

return ','.join(y)

However, when I try to separate the next string it removes the part with numbers.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Input = ’49ersRiders Mapple’

Output = ‘Riders Mapple’

I tried the following code but now it removes the ‘ers’ part.

def test(x):

y = re.findall(r'\d+[A-Z]*|[A-Z][^A-Z\s]+(?:\s+\S[^A-Z\s]*)*', x)

return ','.join(y)

Output = ’49,Riders Mapple’

The output I’m looking for is this:

’49ers,Riders Mapple’

Is it possible to add this indication to my regex?

Thanks in advance

>Solution :

Maybe naive but why don’t you use re.sub:

def func(x):
    return re.sub(r'(?<!\s)([A-Z])', r',\1', x)

inp = '49ersRiders Mapple'
out = func(inp)
print(out)

# Output
49ers,Riders Mapple
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading