Петър обнови решението на 23.04.2014 00:13 (преди над 10 години)
+import re
+
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.text = text
+
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def filtered(self):
+ def replacement(mail):
+ username_length = len(mail.group(2))
+ if username_length < 6:
+ filtered = "[FILTERED]" + str(mail.group(3))
+ else:
+ filtered = (mail.group(2)[:3] +
+ "[FILTERED]" + str(mail.group(3)))
+ return filtered
+ match = self.text
+ if (self.partially_preserve_email_username):
+ match = re.sub(r'(\b([a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200})(@'
+ '([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)\\b)',
+ replacement, match)
+ elif (self.preserve_email_hostname):
+ match = re.sub(r'(\b[a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200}(@'
+ '([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\\b))',
+ r'[FILTERED]\2', match)
+ else:
+ match = re.sub(r'(\b[a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200}@'
+ '([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)\\b',
+ '[EMAIL]', match)
+ if (self.preserve_phone_country_code):
+ match = re.sub(r'(((\b00|\+)[1-9][0-9]{1,2})[\- \(\)]{0,2}'
+ r'([0-9][- \(\)]{0,2}){5,10}[0-9])\b',
+ r"\2 [FILTERED]", match)
+ match = re.sub(r'((\b00|\+)[1-9][0-9]{1,2}[\- \(\)]{0,2}'
+ '([0-9][- \(\)]{0,2}){5,10}[0-9])\\b',
+ "[PHONE]", match)
+ match = re.sub(r'\b(0[- \(\)]{0,2}[1-9][\- \(\)]{0,2}'
+ '([0-9][- \(\)]{0,2}){4,9}[0-9])\\b', '[PHONE]', match)
+ return match
+
+
+class Validations:
+
+ @classmethod
+ def is_email(cls, value):
+ pattern = r'^[a-zA-Z0-9][a-zA-Z0-9+\-_\.]{0,200}@'\
+ r'([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'\
+ r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_phone(cls, value):
+ pattern_local = r'^0[- \(\)]{0,2}[1-9][\- \(\)]{0,2}'\
+ r'([0-9][- \(\)]{0,2}){4,9}[0-9]$'
+ pattern_international = r'^(00|\+)[1-9][0-9]{1,2}[\- \(\)]{0,2}'\
+ r'([0-9][- \(\)]{0,2}){5,10}[0-9]$'
+ match = re.search(pattern_local, value)
+ match_international = re.search(pattern_international, value)
+ return (match is not None or match_international is not None)
+
+ @classmethod
+ def is_hostname(cls, value):
+ pattern = (r'^([0-9a-zA-Z]([0-9a-zA-Z-]{0,61}[0-9a-zA-Z])?\.)+'
+ r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$')
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ pattern = (r'^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?'
+ '|0)\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]?|0)$')
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_number(cls, value):
+ pattern = r'^-?(([1-9][0-9]*(\.[0-9]+)?)|(0(\.[0-9]+)?))$'
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_integer(cls, value):
+ pattern = r'^-?([1-9][0-9]*|0)$'
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_date(cls, value):
+ pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$'
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_time(cls, value):
+ pattern = r'^([01][0-9]|2[0-3])(:[0-5][0-9]){2}$'
+ match = re.search(pattern, value)
+ return match is not None
+
+ @classmethod
+ def is_datetime(cls, value):
+ pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])'\
+ r'[ T]([01][0-9]|2[0-3])(:[0-5][0-9]){2}$'
+ match = re.search(pattern, value)
+ return match is not None