6

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.

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

3

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]

Comments

1

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) )

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.