title: How to include keys and values from a dictionary in a regex expresion in python 3?
body: <p>I need to create and modify regexes that need to match for any key or value inside a dictionary. </p>
  
  <p>Doing this in Ruby is relatively easy, for example, like this:</p>
  
  <pre><code>names = {"John White"=&gt;"一","George Black"=&gt;"二","Mary Jane Fields"=&gt;"三"}
  string = "This involves John White (from now on: 'JW'), George Black (from now on: 'GB') and Mary Jane Fields (from now on: 'MJF'). JW wanted GB to tell MJF that he (GB) was with JW when MJF called JW."
  names.each do |k,v|
      string.gsub! k,v
  end
  names.each_value do |v| 
      short_name = /(#{v}) \(from now on: '(.{2,4})'\)/.match(string).to_a
      string.gsub! short_name[2].to_s, short_name[1].to_s
  end
  puts string
  </code></pre>
  
  <p>This gives the following result:</p>
  
  <pre><code>This involves 一 (from now on: '一'), 二 (from now on: '二') and 三 (from now on: '三'). 一 wanted 二 to tell 三 that he (二) was with 一 when 三 called 一.
  </code></pre>
  
  <p>where all names (the real one and the adhoc name given after "from now on:") were replaced with the value in the hash.</p>
  
  <p>When trying to update a similar program from Ruby into Python, though, I am having trouble inserting key/values in the regex in a re.sub command or similar.
  The beginning of the code works fine if done as follows:</p>
  
  <pre><code>import re 
  names = {"John White":"一","George Black":"二","Mary Jane Fields":"三"}
  string = "This involves John White (from now on: JW), George Black (from now on: GB) and Mary Jane Fields (from now on: MJF). JW wanted GB to tell MJF that he (GB) was with JW when MJF called JW."
  for key,value in names.items():
      string = re.sub(str(key),str(value),string)
  print(string)
  </code></pre>
  
  <p>But the second transformation is tricky since I cannot enter the value into the regex expression. All of the following give different errors or just do not match. For Example, the following creates match objects but does not capture nor modifies the string:</p>
  
  <pre><code>for value in names.values():    
      pattern = re.search(str(value)+' \(from now on: (.{2,4})\)',string)
      string = re.sub(str(pattern),str(value),string)
  </code></pre>
  
  <p>Many other things do not work either. Any way to include calls into dictionary values inside regex expressions? Specifically, in a re.sub command?</p>
  
  <p>Thanks a lot.</p>