I’m trying to make a test for checking whether a sys.argv input matches the RegEx for an IP address…
As a simple test, I have the following…
JavaScript
x
9
1
import re
2
3
pat = re.compile("d{1,3}.d{1,3}.d{1,3}.d{1,3}")
4
test = pat.match(hostIP)
5
if test:
6
print "Acceptable ip address"
7
else:
8
print "Unacceptable ip address"
9
However when I pass random values into it, it returns “Acceptable IP address” in most cases, except when I have an “address” that is basically equivalent to d+
.
Advertisement
Answer
You have to modify your regex in the following way
JavaScript
1
2
1
pat = re.compile("^d{1,3}.d{1,3}.d{1,3}.d{1,3}$")
2
that’s because .
is a wildcard that stands for “every character”