提取带端口IP地址的正则表达式需求:移除非IP冗余内容
Extract Only IP Addresses with Ports Using Regex
To pull out valid IPv4 addresses paired with port numbers from your input text (while discarding all other content), you can use a regex tailored to validate both the IP and port ranges. Here's how to do it:
Strict Validation Regex
This pattern ensures you only match valid IPv4 addresses (0.0.0.0 to 255.255.255.255) and valid ports (1 to 65535):
\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):(?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3})\b
Simplified Regex (For Trusted Input)
If you’re confident your input only contains valid IP:port pairs and don’t need strict validation, this faster pattern works well:
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d{1,5}\b
Example Implementation (Python)
Here’s how to use the strict regex to extract and format your desired output:
import re input_text = "1 138.68.161.60:1080 SOCKS5 HIA United States (New York NY) 138.68.161.60 (DigitalOcean, LLC) 174.64.234.29:17501 45.79.219.154:63189" strict_pattern = r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?):(?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3})\b' matches = re.findall(strict_pattern, input_text) clean_output = ' '.join(matches) print(clean_output) # Output: 138.68.161.60:1080 174.64.234.29:17501 45.79.219.154:63189
Regex Breakdown (Strict Version)
Let’s break down the components to understand how it targets exactly what you need:
\b: Word boundary to avoid partial matches (e.g., an IP fragment inside a longer number)- IPv4 Octet Validation:
(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)25[0-5]: Matches values 250-2552[0-4][0-9]: Matches values 200-249[01]?[0-9][0-9]?: Matches values 0-199 (covers single-digit, two-digit, and three-digit numbers)
(?:...)\.{3}: Repeats the octet pattern three times, each followed by a dot (builds the first three parts of the IPv4 address)- Final Octet: Same validation as above (completes the full IPv4 address)
:: Literal colon separating the IP from the port- Port Validation:
(?:6553[0-5]|655[0-2][0-9]|65[0-4][0-9]{2}|6[0-4][0-9]{3}|[1-5][0-9]{4}|[1-9][0-9]{0,3})- Covers all valid port numbers from 1 to 65535 (excludes port 0, which is reserved)
内容的提问来源于stack exchange,提问作者Alex Loopan




