title: Python - parse IPv4 addresses from string (even when censored)
body: <p><strong>Objective:</strong> Write Python 2.7 code to extract IPv4 addresses from string.</p>
  
  <p><strong>String content example:</strong></p>
  
  <hr>
  
  <p>The following are IP addresses: 192.168.1.1, 8.8.8.8, 101.099.098.000.
  These can also appear as 192.168.1[.]1 or 192.168.1(.)1 or 192.168.1[dot]1 or 192.168.1(dot)1 or 192 .168 .1 .1 or 192. 168. 1. 1. and these censorship methods could apply to any of the dots (Ex: 192[.]168[.]1[.]1).</p>
  
  <hr>
  
  <p>As you can see from the above, I am struggling to find a way to parse through a txt file that may contain IPs depicted in multiple forms of "censorship" (to prevent hyper-linking).</p>
  
  <p>I'm thinking that a regex expression is the way to go. Maybe say something along the lines of; any grouping of four ints 0-255 or 000-255 separated by anything in the 'separators list' which would consist of periods, brackets, parenthesis, or any of the other aforementioned examples. This way, the 'separators list' could be updated at as needed.</p>
  
  <p>Not sure if this is the proper way to go or even possible so, any help with this is greatly appreciated. </p>
  
  <hr>
  
  <p><strong>Update:</strong>
  Thanks to recursive's answer below, I now have the following code working for the above example. It will... </p>
  
  <ul>
  <li>find the IPs</li>
  <li>place them into a list</li>
  <li>clean them of the spaces/braces/etc</li>
  <li>and replace the uncleaned list entry with the cleaned one.</li>
  </ul>
  
  <p><strong>Caveat:</strong> The code below does not account for incorrect/non-valid IPs such as 192.168.0.256 or 192.168.1.2.3
  Currently, it will drop the trailing 6 and 3 from the aforementioned. If its first octet is invalid (ex:256.10.10.10) it will drop the leading 2 (resulting in 56.10.10.10).</p>
  
  <pre><code>import re
  
  def extractIPs(fileContent):
      pattern = r"((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)([ (\[]?(\.|dot)[ )\]]?(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})"
      ips = [each[0] for each in re.findall(pattern, fileContent)]   
      for item in ips:
          location = ips.index(item)
          ip = re.sub("[ ()\[\]]", "", item)
          ip = re.sub("dot", ".", ip)
          ips.remove(item)
          ips.insert(location, ip) 
      return ips
  
  myFile = open('***INSERT FILE PATH HERE***')
  fileContent = myFile.read()
  
  IPs = extractIPs(fileContent)
  print "Original file content:\n{0}".format(fileContent)
  print "--------------------------------"
  print "Parsed results:\n{0}".format(IPs)
  </code></pre>