Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.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 用于匹配IPv4地址的正则表达式_Python_Regex_Python 3.x - Fatal编程技术网

Python 用于匹配IPv4地址的正则表达式

Python 用于匹配IPv4地址的正则表达式,python,regex,python-3.x,Python,Regex,Python 3.x,假设我有3个字符串: str1 = 'Hello my name is Ben and my IP address is 127.1.1.1' str2 = 'Hello my name is Folks and my IP address is 1.2.3.4.5' str3 = 'Hello all, ip addresses: 1.2.3.4.5, 127.1.2.1, 127.2.1.2' 我希望获得以下输出: find_ip(str1) #['127.1.1.1'] find_ip(

假设我有3个字符串:

str1 = 'Hello my name is Ben and my IP address is 127.1.1.1'
str2 = 'Hello my name is Folks and my IP address is 1.2.3.4.5'
str3 = 'Hello all, ip addresses: 1.2.3.4.5, 127.1.2.1, 127.2.1.2'
我希望获得以下输出:

find_ip(str1) #['127.1.1.1']
find_ip(str2) #[]
find_ip(str2) #['127.1.2.1', '127.2.1.2']
  • 标准:
    • IM地址的格式为“x.x.x.x”
    • 正则表达式应为1组
    • 位数不重要(111.111.111.111)是好的

  • p.S的解决方案不回答这个问题。

    请尝试下面的正则表达式。它将获得以127开头的ip地址,然后是3组带点的数字

    re.findall( r'127+(?:\.[0-9]+){3}', str1 )
    re.findall( r'127+(?:\.[0-9]+){3}', str2 )
    re.findall( r'127+(?:\.[0-9]+){3}', str3 )
    
    结果:

    ['127.1.1.1']
    []
    ['127.1.2.1', '127.2.1.2']
    

    以下正则表达式将IP从
    0.0.0.0
    匹配到
    255.255.255.255
    ,前后不带句点或数字:

    (?<![\.\d])(?:[0-9]\.|1\d?\d?\.|2[0-5]?[0-5]?\.){3}(?:[0-9]|1\d?\d?|2[0-5]?[0-5]?)(?![\.\d])
    
    演示:

    完整的Python实现:

    import re
    
    str1 = 'Hello my name is Ben and my IP address is 127.1.1.1'
    str2 = 'Hello my name is Folks and my IP address is 1.2.3.4.5'
    str3 = 'Hello all, ip addresses: 1.2.3.4.5, 127.1.2.1, 127.2.1.2'
    
    def find_ip(test_str):
        regex = re.compile(r"(?<![-\.\d])(?:0{0,2}?[0-9]\.|1\d?\d?\.|2[0-5]?[0-5]?\.){3}(?:0{0,2}?[0-9]|1\d?\d?|2[0-5]?[0-5]?)(?![\.\d])")
        return regex.findall(test_str)
    
    print(find_ip(str1)) #['127.1.1.1']
    print(find_ip(str2)) #[]
    print(find_ip(str3)) #['127.1.2.1', '127.2.1.2']
    
    重新导入
    str1='你好,我的名字是Ben,我的IP地址是127.1.1.1'
    str2='你好,我的名字是Peoples,我的IP地址是1.2.3.4.5'
    str3='大家好,ip地址:1.2.3.4.5127.1.2.1127.2.1.2'
    def查找ip(测试端口):
    
    regex=re.compile(r)(?你确定你永远不需要处理IPv6地址吗?@AkshatMahajan我不需要处理IPv6。可能是的重复项
    import re
    
    str1 = 'Hello my name is Ben and my IP address is 127.1.1.1'
    str2 = 'Hello my name is Folks and my IP address is 1.2.3.4.5'
    str3 = 'Hello all, ip addresses: 1.2.3.4.5, 127.1.2.1, 127.2.1.2'
    
    def find_ip(test_str):
        regex = re.compile(r"(?<![-\.\d])(?:0{0,2}?[0-9]\.|1\d?\d?\.|2[0-5]?[0-5]?\.){3}(?:0{0,2}?[0-9]|1\d?\d?|2[0-5]?[0-5]?)(?![\.\d])")
        return regex.findall(test_str)
    
    print(find_ip(str1)) #['127.1.1.1']
    print(find_ip(str2)) #[]
    print(find_ip(str3)) #['127.1.2.1', '127.2.1.2']