Дарина обнови решението на 23.04.2014 16:00 (преди над 10 години)
+import re
+
+
+def generate_email_username_regular_expression():
+ return r"[0-9a-zA-Z][\w+-.]{0,200}"
+
+
+def generate_email_regular_expression():
+ return generate_email_username_regular_expression() + r"@" +\
+ generate_hostname_regular_expression()
+
+
+def generate_phone_prefix_regular_expression():
+ return r"((?<!([a-zA-Z]|[+]))0(?!0)|(([+]|00)[1-9][0-9]{0,2}))"
+
+
+def generate_phone_real_part_regular_expression():
+ return r"([-( )]{0,2}[0-9]){6,11}"
+
+
+def generate_phone_number_regular_expression():
+ return generate_phone_prefix_regular_expression() +\
+ generate_phone_real_part_regular_expression() + r"$"
+
+
+def generate_hostname_regular_expression():
+ domain_or_subdomain_name = r"([0-9a-zA-Z][1-9a-zA-Z-]{0,62}(?<!-)[.])+"
+ TLD_name = r"[a-zA-Z]{2,3}(([.][a-zA-Z]){2}){0,1}"
+ return domain_or_subdomain_name + TLD_name + r"$"
+
+
+def generate_ip_regular_expression():
+ ip_bytes =\
+ [r"(((0|([1-9][0-9]{0,1}))|(1[0-9][0-9]))|((2[0-4][0-9])|(25[0-5])))"] * 4
+ return r"[.]".join(ip_bytes) + r"$"
+
+
+def generate_integer_regular_expression():
+ return r"(0|([-]{0,1}[1-9][0-9]*))$"
+
+
+def generate_number_regular_expression():
+ return generate_integer_regular_expression()[:-1] + r"([.]([0-9])+)*$"
+
+
+def generate_time_regular_expression():
+ return r"(([01][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])$"
+
+
+def generate_date_regular_expression():
+ return r"[0-9]{4}-((0[1-9])|1[012])-((0[1-9])|([12][0-9])|(3[01]))$"
+
+
+def generate_datetime_regular_expression():
+ return generate_date_regular_expression()[:-1] + r"( |T)" +\
+ generate_time_regular_expression()
+
+
+class PrivacyFilter:
+
+ def __init__(self, text):
+ self.text = text
+ self.filtered_text = ""
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ self.filtered_text = self.text
+ username_regular_expression =\
+ generate_email_username_regular_expression() + r"@"
+ if not self.preserve_email_hostname and not\
+ self.partially_preserve_email_username:
+ self.filtered_text =\
+ re.sub(generate_email_regular_expression()[:-1], "[EMAIL]",
+ self.filtered_text)
+ elif self.preserve_email_hostname and not\
+ self.partially_preserve_email_username:
+ self.filtered_text =\
+ re.sub(username_regular_expression, "[FILTERED]@",
+ self.filtered_text)
+ else:
+ matches =\
+ re.findall(username_regular_expression, self.filtered_text)
+ for match in matches:
+ if len(match) < 6:
+ self.filtered_text = self.filtered_text.replace(
+ match, "[FILTERED]@")
+ else:
+ partially_filtered = match[0:3] + "[FILTERED]@"
+ self.filtered_text =\
+ self.filtered_text.replace(match, partially_filtered)
+ if not self.preserve_phone_country_code:
+ phone_number =\
+ r"(0(?!0)|((\b[+]|00)[1-9][0-9]{1,2}))([-( )]{0,2}[0-9]){6,11}"
+ self.filtered_text =\
+ re.sub(phone_number, "[PHONE]", self.filtered_text)
+ else:
+ local_number_regular_expression = r"0" +\
+ generate_phone_real_part_regular_expression()
+ self.filtered_text = re.sub(local_number_regular_expression,
+ "[PHONE]", self.filtered_text)
+ international_phone_without_code =\
+ r"(?<=(([+]|00)[1-9][0-9][0-9][0-9]))([-( )]{0,2}[0-9]){6,11}"
+ self.filtered_text = re.sub(international_phone_without_code,
+ " [FILTERED]", self.filtered_text)
+ return self.filtered_text
+
+
+class Validations:
+
+ @classmethod
+ def is_email(cls, value):
+ return re.match(generate_email_regular_expression(), value) is not None
+
+ @classmethod
+ def is_phone(cls, value):
+ return re.match(generate_phone_number_regular_expression(), value) is\
+ not None
+
+ @classmethod
+ def is_hostname(cls, value):
+ return re.match(generate_hostname_regular_expression(), value) is\
+ not None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return re.match(generate_ip_regular_expression(), value) is not None
+
+ @classmethod
+ def is_number(cls, value):
+ return re.match(generate_number_regular_expression(), value) is\
+ not None
+
+ @classmethod
+ def is_integer(cls, value):
+ return re.match(generate_integer_regular_expression(), value) is\
+ not None
+
+ @classmethod
+ def is_date(cls, value):
+ return re.match(generate_date_regular_expression(), value) is not None
+
+ @classmethod
+ def is_time(cls, value):
+ return re.match(generate_time_regular_expression(), value) is not None
+
+ @classmethod
+ def is_datetime(cls, value):
+ return re.match(generate_datetime_regular_expression(), value) is not\
+ None