Георги обнови решението на 23.04.2014 02:00 (преди над 10 години)
+import re
+
+
+class Validations:
+
+ @classmethod
+ def is_email(cls, value):
+ mail = value.split('@')
+ res = re.match(r"[a-zA-Z0-9][a-zA-Z0-9_+.-]{,200}", mail[0])
+ valid_user = res.group() == mail[0]
+ valid_host = Validations.is_hostname(mail[1])
+ return valid_user and valid_host
+
+ @classmethod
+ def is_phone(cls, value):
+ try_local = re.match(
+ r"0[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())",
+ value
+ )
+ try_global = re.match(
+ r"(00|\+)((?!0)[0-9]{1,3})" #global prefix
+ r"[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())", #number
+ value)
+ if try_local:
+ if try_local.group() == value:
+ return True
+ if try_global:
+ if try_global.group() == value:
+ return True
+ return False
+
+ @classmethod
+ def is_hostname(cls, value):
+ result = re.match(
+ r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+" #(sub)domain
+ r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?", #TLD
+ value
+ )
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_ip_address(cls, value):
+ result = re.match(
+ r"^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
+ r"(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$",
+ value)
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_number(cls, value):
+ result = re.match(r"-?(0|[1-9][0-9]*)(\.[0-9]+)?", value)
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_integer(cls, value):
+ result = re.match(r"-?(0|[1-9])[0-9]*", value)
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_date(cls, value):
+ expr = r"[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])"
+ result = re.match(expr, value)
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_time(cls, value):
+ expr = r"([01][0-9]|2[0-3]):([0-4][0-9]|5[0-9]):([0-4][0-9]|5[0-9])"
+ result = re.match(expr, value)
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_datetime(cls, value):
+ values = re.split(' |T',value)
+ if len(values) == 2:
+ valid_date = Validations.is_date(values[0])
+ valid_time = Validations.is_time(values[1])
+ return valid_date and valid_time
+ return False
+
+
+class PrivacyFilter:
+
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, text):
+ self.text = text
+
+ def filtered(self):
+ filtered_text = self.text
+ def masK_username(matchobj):
+ email = matchobj.group().split('@')
+ if len(email[0]) < 6:
+ return "[FILTERED]"
+ else:
+ return email[0][:3] + "[FILTERED]@" + email[1]
+
+ if self.partially_preserve_email_username:
+ filtered_text = re.sub(
+ r"([a-zA-Z0-9][a-zA-Z0-9_+.-]{,200})@"
+ r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
+ r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
+ masK_username,
+ filtered_text
+ )
+ elif self.preserve_email_hostname:
+ filtered_text = re.sub(
+ r"([a-zA-Z0-9][a-zA-Z0-9_+.-]{,200})@"
+ r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
+ r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
+ r"[FILTERED]@\2\3",
+ filtered_text
+ )
+ else:
+ filtered_text = re.sub(
+ r"[a-zA-Z0-9][a-zA-Z0-9_+.-]{,200}@"
+ r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
+ r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
+ "[EMAIL]",
+ filtered_text
+ )
+ if self.preserve_phone_country_code:
+ filtered_text = re.sub(
+ r"(00|\+)((?!0)[0-9]{1,3})"
+ r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-()))",
+ r"\1\2 [FILTERED]",
+ filtered_text
+ )
+ else:
+ filtered_text = re.sub(
+ r"(00|\+)((?!0)[0-9]{1,3})"
+ r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-()))",
+ "[PHONE]",
+ filtered_text
+ )
+ filtered_text = re.sub(
+ r"0[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())",
+ '[PHONE]',
+ filtered_text
+ )
+ return filtered_text
+