Nicolas Pogacsas
Nicolas Pogacsas

Reputation: 63

Python regex replace every 2nd occurrence in a string

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.

Upvotes: 6

Views: 826

Answers (4)

Kelly Bundy
Kelly Bundy

Reputation: 27599

Simpler versions of Wiktor's solution, using itertools.cycle instead:

c = cycle((",", "],["))
print( re.sub(r"],\[", lambda x: next(c), str1) )
c = cycle((True, False))
print( re.sub(r"],\[", lambda x: "," if next(c) else x.group(), str1) )

Upvotes: 1

Cubix48
Cubix48

Reputation: 2681

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]

Upvotes: 3

user15398259
user15398259

Reputation:

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]'

Upvotes: 3

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

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.

Upvotes: 6

Related Questions