Home > Net >  Python regex replace every 2nd occurrence in a string
Python regex replace every 2nd occurrence in a string

Time:03-01

I have a string with data that looks like this:

str1 = "[2.4],[5],[2.54],[4],[3.36],[4.46],[3.36],[4],[3.63],[4.86],[4],[4.63]"

I would want to replace every second iteration of "],[" with "," so it will look like this:

str2 = "[2.4,5],[2.54,4],[3.36,4.46],[3.36,4],[3.63,4.86],[4,4.63]"

Here is was I have so far:

str1 = "[2.4],[5],[2.54],[4],[3.36],[4.46],[3.36],[4],[3.63],[4.86],[4],[4.63]"
s2 = re.sub(r"],\[", ',', str1)
print(s2)

I was trying to mess around with this:

(.*?],\[){2}

But it does not seem to yield me the desired results.

I tried using loops but I only managed to replace only the second occurrence and nothing after using this sample code I found here. And the code is:

import re

def replacenth(string, sub, wanted, n):
    where = [m.start() for m in re.finditer(sub, string)][n-1]
    before = string[:where]
    after = string[where:]
    after = after.replace(sub, wanted, 1)
    newString = before   after
    print(newString)
For these variables:

string = 'ababababababababab'
sub = 'ab'
wanted = 'CD'
n = 5

Thank you.

CodePudding user response:

You can use

import re
from itertools import count

str1 = "[2.4],[5],[2.54],[4],[3.36],[4.46],[3.36],[4],[3.63],[4.86],[4],[4.63]"
c = count(0)
print( re.sub(r"],\[", lambda x: "," if next(c) % 2 == 0 else x.group(), str1) )
# => [2.4,5],[2.54,4],[3.36,4.46],[3.36,4],[3.63,4.86],[4,4.63]

See the Python demo.

The regex is the same, ],\[, it matches a literal ],[ text.

The c = count(0) initializes the counter whose value is incremented upon each match inside a lambda expression used as the replacement argument. When the counter is even, the match is replaced with a comma, else, it is kept as is.

CodePudding user response:

You could capture the parts you want to keep.

  1. (\[[^]] ) - capture [ and everything up to but not including the next ]
  2. ],\[ - match ],[
  3. ([^]] ) - capture everything up to but not including next ]
>>> re.sub(r"(\[[^]] )],\[([^]] )", r"\1,\2", str1)
'[2.4,5],[2.54,4],[3.36,4.46],[3.36,4],[3.63,4.86],[4,4.63]'

CodePudding user response:

Here is another way to do it only using regex:

import re

text = '[2.4],[5],[2.54],[4],[3.36],[4.46],[3.36],[4],[3.63],[4.86],[4],[4.63]'

print(re.sub(r'],\[(.*?])', r',\1', text))

Output:

[2.4,5],[2.54,4],[3.36,4.46],[3.36,4],[3.63,4.86],[4,4.63]
  • Related