'replace text between two strings in a file - python

i have a sh file, i need my python script to find specific strings and replace all the text between them in it:

hello_example=( # <- this specific line
"bla"
"bla"
"bla"
"bla"
"bla"
) # <- until this one

i already have the correctly formatted string to replace this with, i just didnt found an effective way to do what i just described so far.

What would be the best solution?



Solution 1:[1]

Try this: check solution here: Regex101

your_input = """hello_example=(
"bla"
"bla"
"bla"
"bla"
"bla"
)"""
re.sub(r"(?<=hello_example\=\().*(?=\))", '', your_input, flags=re.S)

Output:
'hello_example=()'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1