TransWikia.com

Split string by comma unless followed by a space or a '+'

Stack Overflow Asked by Callum Brown on December 30, 2021

I’m trying to split an extremely long string by commas. I have two requirements, however:

  1. the comma cannot be followed by a space
  2. the comma cannot be followed by a ‘+’ symbol

so for example, the input would be:

text = "hello,+how are you?,I am fine, thanks"

and the output of this is:

['hello,+how are you?', 'I am fine, thanks']

i.e. the only comma that seperated the values was the one that was not followed by a ‘+’ or a space

I have managed requirement 1) as follows:

re.split(r',(?=[^s]+)',text)

I cannot figure out how to add requirement 2)

3 Answers

I suggest you go with @HampusLarsson's answer, but I'd like to squeeze in an answer that doesn't use imported modules:

s = "hello,+how are you?,I am fine, thanks"

ind = [0]+[i for i,v in enumerate(s)
           if v == ',' and s[i+1] not in [' ','+']]

parts = [s[i:j].lstrip(',')
         for i,j in zip(ind, ind[1:]+[None])]

print(parts)

Output:

['hello,+how are you?', 'I am fine, thanks']

Answered by Ann Zen on December 30, 2021

The simplest solution is to only look for the pattern that you don't want, and exclude it altogether. You do that using negative-lookahead in regular-expression.

>>> text = "hello,+how are you?,I am fine, thanks"
>>> re.split(r',(?![+ ])', text)
['hello,+how are you?', 'I am fine, thanks']

This will match , unless it's followed either by a literal + or a space.

Answered by Hampus Larsson on December 30, 2021

Try this

re.split(r',(?=[^s +])',text)

Answered by Pratyaksh Saini on December 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP