# Credit Mike Dunderdale for regex
import time
time_start = time.time()
#ip_str
= "255.000.0.111"
ip_str = "23.34.45.45"
import re
def ipFormatChk(ip_str):
pattern = re.compile(r"""
\b #
matches the beginning of the string
(25[0-5]| # matches the
integer range 250-255 OR
2[0-4][0-9]| # matches the
integer range 200-249 OR
[01]?[0-9][0-9]?) # matches any other
combination of 1-3 digits below 200
\. # matches '.'
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # repeat
\. # matches '.'
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # repeat
\.
# matches '.'
(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?) # repeat
\b #
matches the end of the string
""", re.VERBOSE)
if re.match(pattern, ip_str):
return True
else:
return False
for i in range(10000):
i = ipFormatChk(ip_str)
print i
time_end = time.time()
print time_end-time_start