I am trying to build an IPv4 regex in Python. This is what I have:
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
And these are the inputs it misclassified:
Input: "172.316.254.1"
Output: true
Expected Output: false
Input: "1.1.1.1a"
Output: true
Expected Output: false
Input: "1.23.256.255."
Output: true
Expected Output: false
Input: "64.233.161.00"
Output: true
Expected Output: false
Input: "64.00.161.131"
Output: true
Expected Output: false
Input: "01.233.161.131"
Output: true
Expected Output: false
Input: "1.1.1.1.1"
Output: true
Expected Output: false
Input: "1.256.1.1"
Output: true
Expected Output: false
Input: "1.256.1.1"
Output: true
Expected Output: false
Input: "255.255.255.255abcdekjhf"
Output: true
Expected Output: false
This is the code that I have. It basically returns a boolean value:
import re
def isIPv4Address(inputString):
pattern = re.compile(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}')
return pattern.match(inputString) is not None