1

I have a string with repeated consecutive comas, and I want to insert a character between the comas, for example a zero. When I run re.sub what it returns is:

re.sub(r",,", ",0," , "a,,,,,,,,,,,,,a",)
'a,0,,0,,0,,0,,0,,0,,a'

and the result that I want is all the substitutions, like:

'a,0,0,0,0,0,0,0,0,0,0,0,0,a'

I understand why it's is happening, there is overlapping in the pattern, but I don't know how to fix it.

2 Answers 2

3

You can use a lookahead to match a comma that is followed by another comma, and replace matches with ",0", for example:

>>> re.sub(r",(?=,)", ",0", "a,,,,,,,,,,,,,a")
'a,0,0,0,0,0,0,0,0,0,0,0,0,a'

Or alternatively without regular expressions:

>>> ",".join(x or "0" for x in "a,,,,,,,,,,,,,a".split(","))
'a,0,0,0,0,0,0,0,0,0,0,0,0,a'
0
0

This achieves the desired result:

In [1]: re.sub(r",,", ",0,0" , "a,,,,,,,,,,,,,a",)
Out[1]: 'a,0,0,0,0,0,0,0,0,0,0,0,0,a'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.