0

My Issue

I am trying to just return the valid IP (the task only wants 1-255.0-255.0-255.0-255 i.e. no leading 0's)
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.

My Question

Why does the "NONE" get returned after every function call?

What I've done

I've verified my Regex with Regex101.com
I've tried commenting out all but 1 of each of the method calls, but get the same result
I've tried doing this from a python interpreter

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

Which didn't return any 'NONE' value
Lastly, I tried adding extra Print()'s to see when the 'NONE' value was going to be returned.
The best I can determine, the 'NONE' is sent at the end of the function for some reason.
I've been trying to read the Python RE Docs, but can't seem to find a reason listed.

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

-----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

NOTE: I have a Mac, which apparently has specific versions of Python it works with?

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

aRustyDev
  • 101
  • 1
  • 11

1 Answers1

0

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.

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

255.255.255.255
ERROR: Bad Input
192.168.0.55
ERROR: Bad Input
aRustyDev
  • 101
  • 1
  • 11