title: python use re.sub with a dict to replace multi exact substrings
body: <p>In python, I am trying to use re.sub with a <strong>dict</strong> to replace multi &quot;<strong>exact</strong>&quot; substrings. The following is a sample example.</p>
  <pre><code>import re
  
  words = &quot;apple pineapple cat category data old_data&quot;
  dic = {&quot;apple&quot;:&quot;apple_new&quot;, &quot;cat&quot;:&quot;cat_new&quot;, &quot;data&quot;:&quot;data_new&quot;}
  
  pattern = re.compile(&quot;|&quot;.join(dic.keys()))
  
  new_words = re.sub(pattern, lambda m: dic[m.group(0)], words)
  </code></pre>
  <p>The result is :</p>
  <pre><code>apple_new pineapple_new cat_new cat_newegory data_new old_data_new
  </code></pre>
  <p>The result I expected is :</p>
  <pre><code>apple_new pineapple cat_new category data_new old_data
  </code></pre>
  <p>What should I do?
  I have tried <a href="https://stackoverflow.com/questions/31697043/replace-exact-substring-in-python">Replace exact substring in python</a> methods (r&quot;\b...\b&quot;)</p>
  <pre><code>new_words2 = re.sub(r&quot;\bapple\b&quot;, &quot;apple_new&quot;, words)
  </code></pre>
  <p>It can reach my goals. But in my real project, I have to replace about 100 patterns at once. So I hope can use <strong>dict</strong> with r&quot;\bapple\b&quot;</p>
  <p>If you know how to do, please tell me, thanks.</p>