Search code examples
pythonregexregex-lookaroundslookbehind

Regex replace: skip text inside quotes (Python)


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!


Solution

  • 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"