Лъчезар обнови решението на 23.04.2014 00:31 (преди над 10 години)
+import re
+from builtins import classmethod
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.__original_text = text[:]
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+ self.preserve_phone_country_code = False
+
+ def filtered(self):
+ filtered_text = None
+
+ email_pattern = r'([^\W_][\w\.\+-]{0,2})[\w\.\+-]{0,198}(@(?:(?:(?:[' \
+ r'^\W_]{1,2})|(?:[^\W_][a-zA-Z0-9-]{0,61}[^\W_]))\.)' \
+ r'+[a-zA-Z]{2,3})'
+
+ international_phone_pattern = r'((00|\+)[1-9]\d{1,2})([\\(\\) -]{0,' \
+ r'2}\d){6,11}'
+
+ local_phone_pattern = r'(0|\+)([\\(\\) -]{0,2}\d){6,11}'
+
+ if self.partially_preserve_email_username:
+ filtered_text = re.sub(email_pattern, r'\1[FILTERED]\2',
+ self.__original_text)
+ elif self.preserve_email_hostname:
+ filtered_text = re.sub(email_pattern, r'[FILTERED]\2',
+ self.__original_text)
+ else:
+ filtered_text = re.sub(email_pattern, r'[EMAIL]',
+ self.__original_text)
+
+ if self.preserve_phone_country_code:
+ filtered_text = re.sub(international_phone_pattern,
+ r'\1 [FILTERED]',
+ filtered_text)
+ else:
+ filtered_text = re.sub(international_phone_pattern, r'[PHONE]',
+ filtered_text)
+
+ filtered_text = re.sub(local_phone_pattern, r'[PHONE]', filtered_text)
+
+ return filtered_text
+
+
+class Validations:
+ __hostname_pattern = r'^((([^\W_]{1,2})|([^\W_][a-zA-Z0-9-]{0,61}' \
+ r'[^\W_]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
+ __email_pattern = r'^[^\W_][\w\.\+-]' \
+ r'{{0,200}}@{}'.format(__hostname_pattern[1:])
+ __telephone_pattern = r'^((0|\+)|((00|\+)[1-9]\d{1,2}))' \
+ r'([\\(\\) -]{0,2}\d){6,11}$'
+ __date_pattern = r'^\d{4}-((0[1-9])|(1[0-2]))-((0[1-9])|([12]\d)|(3[01]))$'
+ __time_pattern = r'^(([01]\d)|(2[0-3])):[0-5]\d:[0-5]\d$'
+ __integer_pattern = r'^(\+|-)?(0|(?:[1-9]\d*))$'
+ __number_pattern = r'{}{}$'.format(__integer_pattern[:-1], '(\.\d*)?')
+ __byte_pattern = r'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
+ __ip_address_pattern = r'^({0}\.){{3}}{0}$'.format(__byte_pattern)
+ __datetime_pattern = r'{}[T ]' \
+ r'{}'.format(__date_pattern[:-1], __time_pattern[1:])
+
+ def __validate(value, pattern):
+ matched_string = re.match(pattern, value)
+ return matched_string is not None
+
+ @classmethod
+ def is_email(cls, value):
+ return cls.__validate(value, cls.__email_pattern)
+
+ @classmethod
+ def is_phone(cls, value):
+ return cls.__validate(value, cls.__telephone_pattern)
+
+ @classmethod
+ def is_hostname(cls, value):
+ return cls.__validate(value, cls.__hostname_pattern)
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return cls.__validate(value, cls.__ip_address_pattern)
+
+ @classmethod
+ def is_number(cls, value):
+ return cls.__validate(value, cls.__number_pattern)
+
+ @classmethod
+ def is_integer(cls, value):
+ return cls.__validate(value, cls.__integer_pattern)
+
+ @classmethod
+ def is_date(cls, value):
+ return cls.__validate(value, cls.__date_pattern)
+
+ @classmethod
+ def is_time(cls, value):
+ return cls.__validate(value, cls.__time_pattern)
+
+ @classmethod
+ def is_datetime(cls, value):
+ return cls.__validate(value, cls.__datetime_pattern)