Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.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
regexp以匹配python中的IP地址_Python_Regex - Fatal编程技术网

regexp以匹配python中的IP地址

regexp以匹配python中的IP地址,python,regex,Python,Regex,我编写了以下regexp以匹配IP地址 x = 'IP is 200.185.24.24' y = re.findall('([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-2][0-3])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])',x) 我得到了以下答案

我编写了以下regexp以匹配IP地址

x = 'IP is 200.185.24.24'
y = re.findall('([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-2][0-3])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])',x)
我得到了以下答案

>>> y
[('200', '185', '24', '2')]
我这里有两个问题

  • 为什么我的桅杆八重奏在这里失败了。休息很好
  • 当您看到我上面的模式时,我重复了相同的模式以匹配IP地址的第二、第三和第四个八位字节。有没有一种简单的方法来表示这种重复模式。我猜tcl中的\d是用于重复的(不确定)。我正在寻找类似的python

  • 感谢您的输入。

    将正则表达式中的所有捕获组转换为非捕获组。此外,最好使用单词边界,否则必须反转模式,如
    (?:大模式|中模式|小模式)


    您获得上述输出主要是因为
    re.findall
    函数将第一优先权赋予组而不是匹配项。如果没有组,则会优先选择匹配。

    在Regex Cookbook ch 7 at中有几个用于匹配IP地址的示例Regex。谢谢Avinash。我试过你的,但是返回一个空名单,但我试过你的,但是返回一个空名单,>>>>x='IP是200.185.24.24.24'>>>>>>y=y=R.findall(>>>>>>x='x='IP是200.185.24.24.24.24'>>>>>y=y=R.findall('\b=b.b('\b(?[1-1-9[1-9[1-9[1-9[1-9[1-9[1-9[1-9[1-9[1-9][1-9[1-9[1-9[1-9[1-9[1-9[1-1-9[1-1-1-1-9][1-9[1-9[1-9[1-9[1-9[1-9[1-1-1-1-9][1-[1-9][0-9]| 1[0-9][0-9]| 2[0-5][0-5])\b',x)>>>>>y[]。。。。。。。。。。。。。。。。。感谢您的投入。
    y = re.findall('\b(?:[1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-2][0-3])\.(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\.(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-5][0-5])\b',x)