import re
regex = re.compile(r"(person\s*=\s*{[^}]*'name'\s*:\s*')(.*?)(')", flags=re.DOTALL)
test_str = ("person = {\n"
" 'name' : 'Jhon Dow',\n"
" 'address' : 'London',\n"
" 'age' : 26,\n"
" 'isMarried' : True ,\n"
" 'brothersNames' : ['Kim' , 'David' , 'Ron']\n"
"} \n\n"
"animal = {\n"
" 'type' : 'Lion',\n"
" 'name' : 'Simba',\n"
" 'age' : 10,\n"
"}")
subst = "\\1What an ugly design\\3"
result = regex.sub(subst, test_str, count=1)
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