简体   繁体   中英

Replace all the occurrences of consecutive repeated pair of values

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.

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'

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM