title: Python: Replace all substring occurrences with regular expressions
body: <p>I would like to replace all substring occurrences with regular expressions. The original sentences would be like:</p>
  
  <pre><code>mystring = "Carl's house is big. He is asking 1M for that(the house)."
  </code></pre>
  
  <p>Now let's suppose I have two substrings I would like to bold. I bold the words by adding ** at the beginning and at the end of the substring. The 2 substrings are:</p>
  
  <pre><code>substring1 = "house", so bolded it would be "**house**"
  substring2 = "the house", so bolded it would be "**the house**"
  </code></pre>
  
  <p>At the end I want the original sentence like this:</p>
  
  <pre><code>mystring = "Carl's **house** is big. He is asking 1M for that(**the house**)."
  </code></pre>
  
  <p>The main problem is that as I have several substrings to replace, they can overlap words like the example above. If I analyze the longest substring at first, I am getting this:</p>
  
  <pre><code>Carl's **house** is big. He is asking 1M for that(**the **house****). 
  </code></pre>
  
  <p>On the other hand, if I analyze the shortest substring first, I am getting this:</p>
  
  <pre><code>Carl's **house** is big. He is asking 1M for that(the **house**).
  </code></pre>
  
  <p>It seems to be I will need to replace from the longest substring to the shortest, but I wonder how should I do to consider it in the first replacement but in the second. Also remember the substring can appear several times in the string.</p>
  
  <p>Note:// Suppose the string ** will never occur in the original string, so we can use it to bold our words</p>