Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python正则表达式正在从IP字符串中删除端口号_Python_Regex - Fatal编程技术网

Python正则表达式正在从IP字符串中删除端口号

Python正则表达式正在从IP字符串中删除端口号,python,regex,Python,Regex,我有一个文本文件,其中包含的文本和IP与端口号行,我想删除端口号和打印只是IP 示例文本文件: 77.55.211.77:8080 诺普 79.127.57.42:80 期望输出: 77.55.211.77 79.127.57.42 我的代码: import re with open('IPs.txt', 'r') as infile: for ip in infile: ip = ip.strip('\n') IP_without_port_numb

我有一个文本文件,其中包含的文本和IP与端口号行,我想删除端口号和打印只是IP

示例文本文件:

77.55.211.77:8080 诺普 79.127.57.42:80 期望输出:

77.55.211.77 79.127.57.42 我的代码:

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)

我不明白为什么在没有端口号的情况下打印到控制台IP_时,我会将所有行都视为输出

如果不需要正则表达式,请在读取行时使用:字符上的拆分函数。然后,您将得到一个具有两个位置的数组,第一个位置仅包含IP地址,另一个位置包含端口。

如果不需要正则表达式,请在读取行时使用:字符上的拆分函数。然后,您将得到一个具有两个位置的数组,第一个位置仅包含IP地址,另一个位置包含端口。

您只需要第二个匹配:

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])
输出:

77.55.211.77
79.127.57.42
77.55.211.77
79.127.57.42
一艘班轮:

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)

您所需要的只是第二场比赛:

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])
输出:

77.55.211.77
79.127.57.42
77.55.211.77
79.127.57.42
一艘班轮:

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)
试试这个:

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)
输出:

77.55.211.77
79.127.57.42
77.55.211.77
79.127.57.42
试试这个:

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)
输出:

77.55.211.77
79.127.57.42
77.55.211.77
79.127.57.42

我想出了这个正则表达式代码,它对我来说很有用,而且很简单

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

我想出了这个正则表达式代码,它对我来说很有用,而且很简单

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