Ангел обнови решението на 22.04.2014 19:01 (преди над 10 години)
+import re
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.text = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ to_filter = self.text
+
+ user = r'(\b[a-zA-Z0-9][\w\+\.\-]{,200}@)'
+ hostname = r'(((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)'
+ email = user + hostname
+
+ if self.preserve_email_hostname:
+ to_filter = re.sub(email, r'[FILTERED]@\2', to_filter)
+ elif self.partially_preserve_email_username:
+ to_filter = re.sub(r'(\b[a-zA-Z0-9])[\w\+\.\-]{,4}@'+hostname, r'[FILTERED]@\2', to_filter)
+ to_filter = re.sub(r'(\b[a-zA-Z0-9][\w\+\.\-]{2})[\w\+\.\-]{3,198}@'+hostname, r'\1[FILTERED]@\2', to_filter)
+ else:
+ to_filter = re.sub(email, r'[EMAIL]',to_filter)
+
+ if self.preserve_phone_country_code:
+ to_filter = re.sub(r'(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}', r'\1\2 [FILTERED]', to_filter)
+ else:
+ to_filter = re.sub(r'(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}', r'[PHONE]', to_filter)
+ to_filter = re.sub(r'0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}', r'[PHONE]', to_filter)
+
+ return to_filter
+
+class Validations:
+ @classmethod
+ def is_email(klas, value):
+ user = r'^[a-zA-Z0-9][\w\+\.\-]{,200}@'
+ hostname = r'((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
+ email = user + hostname
+ return bool(re.match(email, value))
+
+ @classmethod
+ def is_phone(klas, value):
+ return klas.is_local_phone(value) or klas.is_international_phone(value)
+
+ @classmethod
+ def is_local_phone(klas, value):
+ local = r'^0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}$'
+ return bool(re.match(local, value))
+
+ @classmethod
+ def is_international_phone(klas, value):
+ international = r'^(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}$'
+ return bool(re.match(international, value))
+
+ @classmethod
+ def is_hostname(klas, value):
+ hostname = r'^((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
+ return bool(re.match(hostname, value))
+
+ @classmethod
+ def is_ip_address(klas, value):
+ ipv4 = r'^(([0-2][0-5]{2}|[1]{,1}\d{1,2})\.){3}([0-2][0-5]{2}|[1]{,1}\d{1,2})$'
+ return bool(re.match(ipv4, value))
+
+ @classmethod
+ def is_number(klas, value):
+ number = r'^\-?(0|([1-9]\d*))(\.\d+)?$'
+ return bool(re.match(number, value))
+
+ @classmethod
+ def is_integer(klas, value):
+ integer = r'^\-?(0|([1-9]\d*))$'
+ return bool(re.match(integer, value))
+
+ @classmethod
+ def is_date(klas, value):
+ date = r'^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|(1|2)\d|3[0-1])$'
+ return bool(re.match(date, value))
+
+ @classmethod
+ def is_time(klas, value):
+ time = r'^((0|1)\d|2[0-3]):[0-5]\d:[0-5]\d$'
+ return bool(re.match(time, value))
+
+ @classmethod
+ def is_datetime(klas, value):
+ date = r'^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|(1|2)\d|3[0-1])'
+ time = r'((0|1)\d|2[0-3]):[0-5]\d:[0-5]\d$'
+ datetime = date + '(\ |T)' + time
+ return bool(re.match(datetime, value))