Димитър обнови решението на 23.04.2014 01:11 (преди над 10 години)
+import re
+
+RULES_FOR_FILTRATION = {"EMAIL": "[EMAIL]",
+ "PHONE": "[PHONE]",
+ "PARTIAL": "[FILTERED]"}
+
+HOSTNAME_PATTERN = "(?P<hostname>([a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]\.)+" + \
+ "[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)"
+
+EMAIL_USERNAME_PATTERN = "(?P<username>[a-zA-Z0-9][a-zA-Z0-9_\+\.-]{0,200})"
+EMAIL_PATTERN = EMAIL_USERNAME_PATTERN + "[@]" + HOSTNAME_PATTERN
+
+PHONE_SEPARATOR = "[\s\(\)-]"
+PHONE_CODE_PATTERN = "((?P<code>(00|\+)[1-9][0-9]{0,2})|0)"
+PHONE_BASE_PATTERN = "" + \
+ "(?(code)" + PHONE_SEPARATOR + "?[0-9]" + PHONE_SEPARATOR + "?|" + \
+ "(" + PHONE_SEPARATOR + "?[1-9]" + PHONE_SEPARATOR + "?|" + \
+ PHONE_SEPARATOR + "[0-9]" + PHONE_SEPARATOR + "?))" + \
+ "(" + PHONE_SEPARATOR + "?[0-9]" + PHONE_SEPARATOR + "?){4,9}" + \
+ PHONE_SEPARATOR + "?[0-9]"
+
+PHONE_PATTERN = "(" + PHONE_CODE_PATTERN + PHONE_BASE_PATTERN + ")"
+
+IP_ADDRESS_PATTERN = "((\d|[1-9]\d|1\d{2}|2([0-4][0-9]|5[0-5]))" + \
+ "(\.(\d|[1-9]\d|1\d{2}|2([0-4][0-9]|5[0-5]))){3})"
+
+NUMBER_PATTERN = "([-]?(0|[1-9]\d*)(\.\d+)?)"
+INTEGER_PATTERN = "([-]?(0|[1-9]\d*))"
+DATE_PATTERN = "(\d{4}[-](0\d|1[0-2])[-]([0-2]\d|3[0-1]))"
+TIME_PATTERN = "(([0-1]\d|2[0-3])([:][0-5]\d){2})"
+DATETIME_PATTERN = DATE_PATTERN + "[\s:T-]" + TIME_PATTERN
+
+
+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):
+
+ text = self.text[:]
+
+ text = self.__filter_email(text)
+
+ text = self.__filter_phone(text)
+
+ return text
+
+ def __filter_email(self, text):
+
+ if self.partially_preserve_email_username:
+ return re.sub(EMAIL_PATTERN,
+ lambda match:
+ RULES_FOR_FILTRATION['PARTIAL'] + "@"
+ + match.group("hostname")
+ if len(match.group("username")) < 6
+ else match.group("username")[0:3] +
+ RULES_FOR_FILTRATION['PARTIAL'] + "@"
+ + match.group("hostname"),
+ text)
+ if self.preserve_email_hostname:
+ return re.sub(EMAIL_PATTERN,
+ RULES_FOR_FILTRATION['PARTIAL'] + r"@\g<hostname>",
+ text)
+ if not(self.preserve_email_hostname
+ or self.partially_preserve_email_username):
+ return re.sub(
+ EMAIL_PATTERN, RULES_FOR_FILTRATION['EMAIL'], text)
+
+ def __filter_phone(self, text):
+
+ if self.preserve_phone_country_code:
+ return re.sub("(?<=[\s\(\)-])" + PHONE_PATTERN,
+ lambda match:
+ RULES_FOR_FILTRATION["PHONE"]
+ if match.group("code") is None
+ else match.group("code") + " "
+ + RULES_FOR_FILTRATION['PARTIAL'],
+ text)
+ else:
+ return re.sub(
+ "(?<=[\s\(\)-])" + PHONE_PATTERN, RULES_FOR_FILTRATION['PHONE'], text)
+
+
+class Validations:
+ #
+
+ @classmethod
+ def is_email(cls, value):
+ if re.search("^" + EMAIL_PATTERN + "$", value) is None:
+ return False
+ return True
+
+ @classmethod
+ def is_phone(cls, value):
+ if re.search("^" + PHONE_PATTERN + "$", value) is None:
+ return False
+ return True
+
+ @classmethod
+ def is_hostname(cls, value):
+ if re.search("^" + HOSTNAME_PATTERN + "$", value) is None:
+ return False
+ return True
+
+ @classmethod
+ def is_ip_address(cls, value):
+ if re.search("^" + IP_ADDRESS_PATTERN + "$", value) is None:
+ return False
+ return True
+
+ @classmethod
+ def is_number(cls, value):
+ if re.search("^" + NUMBER_PATTERN + "$", value) is None:
+ return False
+ return True
+
+ @classmethod
+ def is_integer(cls, value):
+ if re.search("^" + INTEGER_PATTERN + "$", value) is None:
+ return False
+ return True
+
+ @classmethod
+ def is_date(cls, value):
+ if re.search("^" + DATE_PATTERN + "$", value) is None:
+ return False
+ return True
+
+ @classmethod
+ def is_time(cls, value):
+ if re.search("^" + TIME_PATTERN + "$", value) is None:
+ return False
+ return True
+
+ @classmethod
+ def is_datetime(cls, value):
+ if re.search("^" + DATETIME_PATTERN + "$", value) is None:
+ return False
+ return True