title: Find a string in a file based on a pattern and replace it with something else
body: <p>In a file, I want to find a specific content(lines or lines) based on a string pattern, make changes to it, and replace it. The pattern can appear multiple times.
  The file can contain code, (python or c).</p>
  
  <p>The string pattern can have multiple forms so I thought using regex, like:</p>
  
  <pre><code>custom_log("lorem ipsum can be anything ....")
  </code></pre>
  
  <p>or</p>
  
  <pre><code>custom_log("lorem ipsum"
             "can be anything") 
  </code></pre>
  
  <p>or</p>
  
  <pre><code>custom_log("""lorem ipsum
             can be anything""") 
  </code></pre>
  
  <p>The quotes can be simple or double.</p>
  
  <p>I start by going line by line, and search for a pattern</p>
  
  <pre><code>with open(filepath, mode="r") as f:
    for line in f.readlines():
         if "pattern" in line
  </code></pre>
  
  <p>but the issue, is that the content that I search it can be one one line or multiple lines, and between quotes can be anything as a string.</p>
  
  <p>I can't use a simple replace, because I need to get the content, passed to a function, change/adapt the content and pass it back to be written to file.</p>
  
  <p>I want to keep the initial formatting of the file.</p>