Last modified: Apr 27, 2025 By Alexander Williams
Extract IP Addresses from Text in Python
Working with text data often requires extracting specific patterns. One common task is finding IP addresses in logs, files, or strings.
Python's regular expressions (regex) make this easy. This guide shows how to extract both IPv4 and IPv6 addresses from text.
Understanding IP Address Formats
Before extracting IPs, know their formats. IPv4 uses four octets (e.g., 192.168.1.1). IPv6 uses eight groups of hex digits (e.g., 2001:0db8:85a3::8a2e:0370:7334).
You can validate IP addresses after extraction to ensure correctness.
Python Regex for IPv4 Extraction
Here's a Python function to extract IPv4 addresses using regex:
import re
def extract_ipv4(text):
# Regex pattern for IPv4 addresses
pattern = r'\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'
return re.findall(pattern, text)
# Example usage
sample_text = "Server IPs: 192.168.1.1 and 8.8.8.8"
print(extract_ipv4(sample_text))
['192.168.1.1', '8.8.8.8']
The pattern matches numbers 0-255 in each octet. The re.findall
function returns all matches.
Python Regex for IPv6 Extraction
Extracting IPv6 addresses requires a more complex pattern:
def extract_ipv6(text):
# Regex pattern for IPv6 addresses
pattern = r'(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}|[0-9a-fA-F]{1,4}::(?:[0-9a-fA-F]{1,4}:){0,5}[0-9a-fA-F]{1,4}'
return re.findall(pattern, text)
# Example usage
sample_text = "IPv6 addresses: 2001:0db8:85a3::8a2e:0370:7334 and ::1"
print(extract_ipv6(sample_text))
['2001:0db8:85a3::8a2e:0370:7334', '::1']
This pattern handles full, compressed, and mixed IPv6 notation. For more IP operations, see getting local network IPs.
Combined IPv4 and IPv6 Extraction
To extract both IP versions at once, combine the patterns:
def extract_all_ips(text):
ipv4_pattern = r'\b(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b'
ipv6_pattern = r'(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}|[0-9a-fA-F]{1,4}::(?:[0-9a-fA-F]{1,4}:){0,5}[0-9a-fA-F]{1,4}'
return re.findall(ipv4_pattern, text) + re.findall(ipv6_pattern, text)
# Example usage
sample_text = "IPs: 192.168.1.1, 2001:0db8:85a3::8a2e:0370:7334"
print(extract_all_ips(sample_text))
['192.168.1.1', '2001:0db8:85a3::8a2e:0370:7334']
Practical Applications
Extracting IPs is useful for:
- Log analysis
- Network monitoring
- Security scanning
After extraction, you might want to find hostnames from IPs or check port availability.
Conclusion
Python's regex provides powerful tools for IP address extraction. The examples above handle both IPv4 and IPv6 formats.
Remember to validate extracted IPs before use. For advanced networking, explore async IP connections in Python.