【发布时间】:2020-08-18 07:21:02
【问题描述】:
我正在尝试在 Python 中构建 IPv4 正则表达式。这就是我所拥有的:
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
这些是它错误分类的输入:
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
这是我拥有的代码。它基本上返回一个布尔值:
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
【问题讨论】: