import re
regex = re.compile(r"""
(\w+):\s*
(
"[^"]*",? # qualquer coisa entre aspas, vírgula opcional
|[^\n\w]+ # só pra pegar o [{ depois de options
)
""", flags=re.MULTILINE | re.VERBOSE)
test_str = (" options: [{\n"
" value: \"120\",\n"
" id: \"40\",\n"
" title: \"1400g\",\n"
" name: \"Tamanho: 123\"\n"
" },\n"
" {\n"
" value: \"336\",\n"
" id: \"60\",\n"
" title: \"Chocolate\",\n"
" name: \"Sabor\"\n"
" }\n"
" ]")
subst = "\\\"$1\\\": $2"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html