1

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"

4 Answers 4

3

All you need is the second match:

import re

with open('IPs.txt', 'r') as infile:
    for ip in infile:
        re_for_IP = re.match(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', ip)
        if re_for_IP:
            print(re_for_IP[0])

Output:

77.55.211.77
79.127.57.42

One-liner:

import re

ips = []

with open('IPs.txt', 'r') as infile:
    ips = [ip[0] for ip in [re.match(r'(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})', ip) for ip in infile] if ip]

print(ips)
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need regex, use the split function on the : character when reading the line. Then you would be left with an array with two positions, the first containing only the IP address and the other containing the port.

Comments

0

Try this:

import re
regex = '''^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 
            25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 
            25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.( 
            25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$'''

with open('IP.txt', 'r') as infile: 
    for ip in infile:
        ip = ip.strip('\n')
        IP_without_port_number = re.sub(r':.*$', "", ip)
        re_for_IP = re.match(r'^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$',ip)
        if(re.search(regex, IP_without_port_number)):  
            print(IP_without_port_number)

Output:

77.55.211.77
79.127.57.42

Comments

0

I came up wit this regex code, it works for me and its easy.

import re
text = input("Input text: ")
pattern = re.findall(r'\d+\.\d+\.\d+\.\d+', text)
print(pattern)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.