Радослав обнови решението на 23.04.2014 04:26 (преди над 10 години)
+import re
+
+
+class PrivacyFilter:
+ REGEX_EMAIL = re.compile(r'\b([a-zA-Z0-9][a-zA-Z0-9_+.-]{0,200})@((?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?)\b')
+ REGEX_PHONE = re.compile(r'(\b0(?!0)|(?:\b00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}\b')
+ REGEX_PHONE_INTL = re.compile(r'^(?:(?:00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}$')
+
+ 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):
+ filtered_text = self.text
+
+ # First we obfuscate the emails...:
+
+ if self.partially_preserve_email_username:
+ matches = PrivacyFilter.REGEX_EMAIL.finditer(filtered_text)
+
+ for match in matches:
+ username = match.group(1)
+
+ if len(username) >= 6:
+ filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'{}[FILTERED]@\2'.format(username[:3]), filtered_text, 1)
+ else:
+ filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[FILTERED]@\2', filtered_text, 1)
+ elif self.preserve_email_hostname:
+ filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[FILTERED]@\2', filtered_text)
+ else:
+ filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[EMAIL]', filtered_text)
+
+ # ...and then we obfuscate the phone numbers:
+
+ if self.preserve_phone_country_code:
+ matches = PrivacyFilter.REGEX_PHONE.finditer(filtered_text)
+
+ for match in matches:
+ if PrivacyFilter.REGEX_PHONE_INTL.match(match.group()): # ...if the phone number is international...
+ filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'\1 [FILTERED]', filtered_text, 1)
+ else:
+ filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'[PHONE]', filtered_text, 1)
+ else:
+ filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'[PHONE]', filtered_text)
+
+ return filtered_text
+
+
+class Validations:
+ REGEX_EMAIL = re.compile(r'^(?:[a-zA-Z0-9][a-zA-Z0-9_+.-]{0,200})@(?:(?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?)$')
+ REGEX_PHONE = re.compile(r'^(?:0(?!0)|(?:00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}$')
+ REGEX_HOSTNAME = re.compile(r'^(?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?$')
+ REGEX_IP_ADDRESS = re.compile(r'^(?:(?:0|[1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:0|[1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])$')
+ REGEX_NUMBER = re.compile(r'^-?(?:0|[1-9]\d*)(?:\.\d+)?$')
+ REGEX_INTEGER = re.compile(r'^-?(?:0|[1-9]\d*)$')
+ REGEX_DATE = re.compile(r'^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$')
+ REGEX_TIME = re.compile(r'^(?:[01]\d|2[0-3])(?::[0-5]\d){2}$')
+ REGEX_DATETIME = re.compile(r'^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])[ T](?:[01]\d|2[0-3])(?::[0-5]\d){2}$')
+
+ @classmethod
+ def is_email(cls, value):
+ return bool(cls.REGEX_EMAIL.match(value))
+
+ @classmethod
+ def is_phone(cls, value):
+ return bool(cls.REGEX_PHONE.match(value))
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(cls.REGEX_HOSTNAME.match(value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(cls.REGEX_IP_ADDRESS.match(value))
+
+ @classmethod
+ def is_number(cls, value):
+ return bool(cls.REGEX_NUMBER.match(value))
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(cls.REGEX_INTEGER.match(value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(cls.REGEX_DATE.match(value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(cls.REGEX_TIME.match(value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ return bool(cls.REGEX_DATETIME.match(value))