1

I am trying to replace all : by = except the : inside the quotes. In other words: replace all : which are not surrounded by "something and something".

# input
x:123; y:"z:456"

# desired output
x=123; y="z:456"

I tried to solve this using negative lookbehind/lookahead, but I cannot match the text surrounding the : in the quotes, as quantifiers are not allowed in the lookbehind; so this does not work:

re.sub(r'(?<!".+?):(?!.+?")', '$')

Can this be done using regex at all?

Thanks!

foobar
  • 61
  • 4

1 Answers1

2

You might use a capture group with an alternation using re.sub

In the callback check for capture group 1. If it is there, return it. Else return a =

("[^"]*")|:

See a regex demo and a Python demo

import re

pattern = r'("[^"]*")|:'
s = 'x:123; y:"z:456"'

res = re.sub(pattern, lambda x: x.group(1) if x.group(1) else "=", s)
print(res)

Output

x=123; y="z:456"
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
  • 1
    Cools solution. Works for me. Thanks! BTW: For others interested in this: A similar use case was actually discussed [here](https://stackoverflow.com/questions/62110671/replace-comma-outside-double-quote-python-regex) (which I did not find initially). – foobar Jun 18 '21 at 06:53