Python Regex For IP Address Matching

computer scripting on a computer screen

I was working on a Python script to find IP Addresses from a large number of JSON files. Didn’t need anything complex so my script works as follows:

  • Read in all of the JSON files in a given directory.
  • Using Regular Expressions, search the data previously read and attempt to match for IP Addresses.
  • Save a list of suspected IP Addresses to a list.
  • Check each IP Address in the list using the IPStack API. Remove any IP Addresses from the list that are not actually IP Addresses. (This gives us additional info, as well as validates what we found was an actual IP Address.)
  • Output a list of information regarding the IP Address.

This post is to provide a confirmed RegEx for IP Addresses using Python. Without further ado.

  ip_regex = r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"