Калоян обнови решението на 22.04.2014 13:17 (преди над 10 години)
+import re
+
+
+class Patterns:
+ host = ("((([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]{1})|"
+ "([a-zA-Z0-9]{1}))"
+ "((\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]{1}){0,}|"
+ "(\.[a-zA-Z0-9]{1}){0,})"
+ "(\.[a-zA-Z]{2,3})(\.[a-zA-Z]{2,3}){0,1})")
+
+ email = "(([a-zA-Z0-9]{1}[a-zA-Z0-9_+.-]{0,200})@" + host + ")"
+
+ phone = ("^((0[ \-()]{0,1}[123456789]{1}[ \-()]{0,1}"
+ "([0-9]{1}[ \-()]{0,1}){4,9}"
+ "[0-9]{1})|"
+ "((00)|\+)[123456789]{1,3}[ \-()]{0,1}[0-9]{1}[ \-()]{0,1}"
+ "([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})$")
+
+ national_rest_of_phone = ("^([0-9]{1}[ \-()]{0,1}"
+ "([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1}$")
+
+ national_phone = ("(((00)|\+)[123456789]{1,3}[ \-()]{0,1}[0-9]{1}"
+ "[ \-()]{0,1}([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})")
+
+ country_phone = ("(0[ \-()]{0,1}[123456789]{1}[ \-()]{0,1}"
+ "([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})")
+
+ county_phone_code = "(((00)|\+)[123456789]{1,3})"
+
+ hostname = "[a-zA-Z0-9]{1}[a-zA-Z0-9_+.-]{0,200}"
+
+ ip = ("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
+ "([ (\[]?(\.|dot)[ )\]]?(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})$")
+
+ number = "^[\-]{0,1}(0|[123456789]{1})[0-9]{0,}(\.[0-9]{1,}){0,1}$"
+
+ integer = "^[\-]{0,1}(0|[123456789]{1})[0-9]{0,}$"
+
+ date = ("(((19|20)([2468][048]|[13579][26]|0[48])|2000)"
+ "[\-]02[\-]29|((19|20)[0-9]{2}[\-](0[4678]|1[02])"
+ "[\-](0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}[\-](0[1359]|11)"
+ "[\-](0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}[\-]02[\-]"
+ "(0[1-9]|1[0-9]|2[0-8])))")
+
+ time = "(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)"
+
+ date_time = date + "[ T]" + time
+
+class PrivacyFilter:
+
+ preserve_phone_country_code = False
+
+ preserve_email_hostname = False
+
+ partially_preserve_email_username = False
+
+ phone = '[PHONE]'
+
+ email_string = "[EMAIL]"
+
+ filtered_string = "[FILTERED]"
+
+ def __init__(self, text):
+ self.text = text
+ self.preserve_phone_country_code
+
+ def filtered(self):
+ if self.preserve_email_hostname == True:
+ match_object = re.search(Patterns.email , self.text)
+ if match_object != None:
+ for match in match_object.groups():
+ if match != None and re.match("^" + Patterns.email + "$",
+ match + "") != None:
+ match_object = re.search(("("+
+ Patterns.hostname+"@"+
+ ")"), match)
+ if match_object != None:
+ for match in match_object.groups():
+ tmp= self.filtered_string + "@"
+ self.text = self.text.replace(match,tmp)
+
+ if self.partially_preserve_email_username == True:
+ match_object = re.search(Patterns.email , self.text)
+ if match_object != None:
+ for match in match_object.groups():
+ if match != None and re.match("^" + Patterns.email + "$",
+ match + "") != None:
+ host_plus_at = re.match("(" +
+ Patterns.hostname + "@" + ")",
+ match + "").group(1)
+ if host_plus_at.__len__() >= 7:
+ prefix_of_mail = (host_plus_at[:3] +
+ self.filtered_string + "@")
+ tmp = match.replace(host_plus_at, prefix_of_mail)
+ self.text = self.text.replace(match, tmp)
+ else:
+ prefix_of_mail = self.filtered_string + "@"
+ tmp = match.replace(host_plus_at, prefix_of_mail)
+ self.text = self.text.replace(match, tmp)
+
+ self.text = re.sub(Patterns.email, self.email_string , self.text)
+ self.text = re.sub(Patterns.country_phone, self.phone, self.text)
+
+ if self.preserve_phone_country_code == True:
+ match_object = re.search(Patterns.national_phone , self.text)
+ if match_object != None:
+ for match in match_object.groups():
+ if match != None and re.match(("^" +
+ Patterns.national_phone+
+ "$"), match + "") != None:
+ tmp = (re.search(Patterns.county_phone_code,
+ match).group(1)
+ + " " + self.filtered_string)
+ self.text = self.text.replace(match, tmp)
+ return self.text
+
+class Validations:
+ @staticmethod
+ def is_phone(phone):
+ if re.match(Patterns.phone, phone) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_email(email):
+ if re.match("^" + Patterns.email + "$" , email) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_hostname(hostname):
+ if re.match("^" + Patterns.host + "$", hostname) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_number(number):
+ if re.match(Patterns.number, number) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_integer(integer):
+ if re.match(Patterns.integer, integer) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_time(time):
+ if re.match("^" + Patterns.time + "$", time) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_ip_address(ip):
+ if re.match(Patterns.ip, ip) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_date(date):
+ if re.match("^" + Patterns.date + "$", date) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_datetime(date_time):
+ if re.match("^" + Patterns.date_time + "$", date_time) != None:
+ return True
+ else:
+ return False