官网链接 https://docs.python.org/zh-cn/3/library/re.html
re 判断ip是否正确
def check_ip(ipAddr): compile_ip=re.compile('^(1d{2}|2[0-4]d|25[0-5]|[1-9]d|[1-9]).(1d{2}|2[0-4]d|25[0-5]|[1-9]d|d).(1d{2}|2[0-4]d|25[0-5]|[1-9]d|d).(1d{2}|2[0-4]d|25[0-5]|[1-9]d|d)$') if compile_ip.match(ipAddr): return True else: return False
re 判断用户输入的域名是否正确
def is_valid_domain(domain): pattern = re.compile( r'^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|' r'([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|' r'([a-zA-Z0-9][-_.a-zA-Z0-9]{0,61}[a-zA-Z0-9])).' r'([a-zA-Z]{2,13}|[a-zA-Z0-9-]{2,30}.[a-zA-Z]{2,3})$' ) return True if pattern.match(domain) else False
re判断用户输入的url是否正确
regex = re.compile( r'^(?:http|ftp)s?://' # http:// or https:// r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?.)+(?:[A-Z]{2,6}.?|[A-Z0-9-]{2,}.?)|' #domain... r'localhost|' #localhost... r'd{1,3}.d{1,3}.d{1,3}.d{1,3})' # ...or ip r'(?::d+)?' # optional port r'(?:/?|[/?]S+)$', re.IGNORECASE) if re.match(regex, domain) is not None: return True
re 取出用户输入url的域名或者ip
def get_domain(doamin): domain_new = re.findall('[^/">]{3,50}.com|[^/">]{4,50}.cn|[^/"><]{3,50}.net.cn', doamin) # domain_new=re.findall('[^/"><.]{3,20}.com|[^/"><.]{4,}.cn|[^/"><.]{3,20}.net.cn', doamin) try: # print(domain_new,'new',doamin) if not domain_new: reg = re.compile(r'(d{1,3}.d{1,3}.d{1,3}.d{1,3})') item = re.findall(reg, doamin) if not item: return False return item[0] return domain_new[0] except Exception: return False