I have text file which contains lines of text and IPs with port number and I want to remove port number and print just IP.
Example text file:
- 77.55.211.77:8080
- NoIP
- 79.127.57.42:80
Desired output:
- 77.55.211.77
- 79.127.57.42
My code:
import re
with open('IPs.txt', 'r') as infile:
for ip in infile:
ip = ip.strip('\n')
IP_without_port_number = re.sub(r'((?::))(?:[0-9]+)$', "", ip)
re_for_IP = re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$',ip)
print(IP_without_port_number)
I am not understand why I see all lines as output when I am printing to console "IP_without_port_number"