title: How to perform in place replacement of multiple substrings in python
body: <p>I have the a user supplied string which contains one or more IP addresses I need to augment this with the reverse dns lookup for each. My code currently works fine for a single IP and I'm looking to make it capable of performing the replacement on multiple IPs in the string. What would be the best way of doing this?</p>
  <pre><code>import re
  import socket
  
  line = &quot;this is my IP address 8.8.8.8 and my mac address is FF:FF:FF:FF:FF:FF.&quot;;
  print(&quot;Before: &quot; + line)
  ip = re.findall( r'[0-9]+(?:\.[0-9]+){3}', line)
  try:
      domain_name = socket.gethostbyaddr(ip[0])[0]
  except socket.gaierror:
      domain_name = &quot;No Reverse&quot;
  except socket.herror:
      domain_name = &quot;InvalidIP&quot;
  
  ipd = ip[0] + &quot; [&quot; + domain_name + &quot;]&quot;
  line = line.replace(ip[0], ipd)
  print(&quot;After: &quot; + line)
  </code></pre>