简体   繁体   English

Python RE lib - 返回匹配和无

[英]Python RE lib - returns match and NONE

My Issue我的问题

I am trying to just return the valid IP (the task only wants 1-255.0-255.0-255.0-255 ie no leading 0's)我试图只返回有效的 IP (任务只需要 1-255.0-255.0-255.0-255 即没有前导 0)
My Regex is matching just fine, and I have a much less sturdy solution to get the question "right" but I am trying to understand why I'm getting the output I am.我的正则表达式匹配得很好,而且我有一个不太可靠的解决方案来解决“正确”的问题,但我试图理解为什么我得到的是 output。

My Question我的问题

Why does the "NONE" get returned after every function call?为什么每次 function 调用后都会返回“NONE”?

What I've done我做了什么

I've verified my Regex with Regex101.com我已经用 Regex101.com 验证了我的正则表达式
I've tried commenting out all but 1 of each of the method calls, but get the same result我已经尝试注释掉除 1 之外的所有方法调用,但得到相同的结果
I've tried doing this from a python interpreter我已经尝试从 python 解释器执行此操作

>> rev = re.compile(r"(?P<octet>((((2[0-4]\d)|(25[0-5])).?){4}))") >> rev = re.compile(r"(?P<octet>((((2[0-4]\d)|(25[0-5])).?){4}))")
>> x = rev.search("255.255.255.255") >> x = rev.search("255.255.255.255")
>> x.group("octet") >> x.group("八位字节")
'255.255.255.255' '255.255.255.255'
>> print(x.group("octet")) >> 打印(x.group(“八位字节”))
255.255.255.255 255.255.255.255

Which didn't return any 'NONE' value没有返回任何“NONE”值
Lastly, I tried adding extra Print()'s to see when the 'NONE' value was going to be returned.最后,我尝试添加额外的 Print() 以查看何时返回“NONE”值。
The best I can determine, the 'NONE' is sent at the end of the function for some reason.我能确定的最好的结果是,由于某种原因,在 function 末尾发送了“NONE”。
I've been trying to read the Python RE Docs , but can't seem to find a reason listed.我一直在尝试阅读Python RE Docs ,但似乎找不到列出的原因。

My Code我的代码

import re
def ip_checker(var):
    print("-----1------")
    rev = re.compile(r"(?P<octet>(^([^0])(((25[0-5])|(2[0-4][0-9])|(1?[0-9][0-9])|([0-9]))\.?){4}$))")
    try:
        print("-----2------")
        x = rev.match(var)
        print(x.group("octet"))
        print("-----3a------")
    except:
        print("No Results")
        print("-----3b------")

#Method Calls
print(ip_checker("255.255.255.255")) # Returns True
print(ip_checker("0.0.0.0"))         # Returns False
print(ip_checker("192.168.0.55"))    # Returns True
print(ip_checker("0.156.37.15"))     # Returns False

My Output我的 Output

-----1------
-----2------
255.255.255.255
-----3a------
None
-----1------
-----2------
No Results
-----3b------
None
-----1------
-----2------
192.168.0.55
-----3a------
None
-----1------
-----2------
No Results
-----3b------
None

My Python Version我的 Python 版本

NOTE: I have a Mac, which apparently has specific versions of Python it works with?注意:我有一台 Mac,它显然有特定版本的 Python 可以使用?

me@hostname dir % python3 --version
Python 3.6.3

Just to make sure I'm completely up to date只是为了确保我完全是最新的

me@hostname dir % brew update && brew upgrade python
---
#Stuff I'm not gonna show...
#But it updated Portable Ruby & Python 
---
==> Upgrading 1 outdated package:
python 3.7.6_1 -> 3.7.7
---
me@hostname dir % python3 --version
Python 3.7.7

Even after the update, no changes were seen in the output即使在更新之后,output 也没有变化

I found the answer, and its a face-palm type of answer...我找到了答案,它是一个掌上型答案...

I am trying to print the output of the function, but am not returning any output.我正在尝试打印 function 的 output,但没有返回任何 output。

Corrected Code更正的代码

import re
def ip_checker(var):
    rev = re.compile(r"(?P<octet>(^([^0])(((25[0-5])|(2[0-4][0-9])|(1?[0-9][0-9])|([0-9]))\.?){4}$))")
    try:
        x = rev.match(var)
        return x.group("octet")
    except:
        return "No Results"

#Method Calls
print(ip_checker("255.255.255.255")) # Returns True
print(ip_checker("0.0.0.0"))         # Returns False
print(ip_checker("192.168.0.55"))    # Returns True
print(ip_checker("0.156.37.15"))     # Returns False

Output of Corrected Code更正代码的 Output

255.255.255.255
ERROR: Bad Input
192.168.0.55
ERROR: Bad Input

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM