Tuesday 1 October 2024

regex 101, python3 recursively replace single quote inside of double quote

 import re


# Input string

input_str = [{'a': 'b', 'c': "ddd'sss", 'x': "text'with'quotes"}]

input_str = str(input_str)


# Regex pattern to find double quotes and then identify single quotes inside

pattern = r'("([^"]*?)\'([^"]*?)")'


# Replace single quotes with <test> inside double quotes using a lambda function

fixed_output_str = re.sub(pattern, lambda m: m.group(1).replace("'", "<test>"), input_str)


# Displaying the result

print("Output string with single quotes replaced:", fixed_output_str)

No comments:

Post a Comment