首页 > 解决方案 > 为什么这个正则表达式只给出第三个八位字节?

问题描述

    ip = 'The management ip is 10.0.55.0 and the remote ip is 192.167.13.4.\n1.1.22.1 Random sentence'
    pat = re.compile('(\d{1,3}\.){3}')                                                          
    ips = re.findall(pat,ip)
    >>> ips                                                                                          
    ['55.', '13.', '22.']

我本来希望输出是 ['10.0.55.','192.168.13.','1.1.22.']

标签: pythonregex

解决方案


findall返回任何捕获匹配组的内容,如果有的话 - 你这样做。将匹配组更改为非捕获:

pat = re.compile('(?:\d{1,3}\.){3}')

推荐阅读