Теодор обнови решението на 21.04.2014 01:23 (преди над 10 години)
+import re
+phone = (r"((?<=\A)(?<=\W)(?<!\+))0(?!0)(([\-\s\(\)]){0,2}\d){6,11}|"
+ r"((((?<=\b)(0{2}))|(\+))(?!0)(\d){1,3}(([\-\s\(\)]){0,2}\d){6,11})")
+local = r"((?<=\A)|(?<=\W)|(?<!\+))0(?!0)(([\-\s\(\)]){0,2}\d){6,11}"
+inter = r"((((?<=\b)(0{2}))|(\+))(?!0)(\d){1,3}(([\-\s\(\)]){0,2}\d){6,11})"
+country_code = r"(?:0{2}|\+)(?!0)\d{1,3}"
+email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
+ "(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)")
+user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
+domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}"
+ "(?:.(?:[a-zA-Z]){2})?)")
+ip = (r"((([0-9]\.)|([0-9][1-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
+ r"([2][5][0-5]\.))){3}"
+ r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))")
+number = r"(\-)?(0*|[1-9][0-9]*)((\.([0-9])+)?)$"
+integer = r"(\-)?(0*|[1-9][0-9]*)$"
+date = r"([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1]))"
+time = r"(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]"
+datetime = date + r"[\sT]" + time
+domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
+ '(?:.(?:[a-zA-Z]){2})?)$')
+
+
+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):
+ #deal with phone numbers
+ modified = self.text
+ modified = re.sub(local, "[PHONE]", modified)
+ if self.preserve_phone_country_code:
+ modified = re.sub(inter, "{} [FILTERED]", modified). \
+ format(*re.findall(country_code, self.text))
+ #deal with emails
+ if self.preserve_email_hostname:
+ modified = re.sub(email, "[FILTERED]{}", modified). \
+ format(*re.findall(domain, self.text))
+ elif self.partially_preserve_email_username:
+ host = [item[:3] + "[FILTERED]" + _ for item in
+ re.findall(user, modified)
+ for _ in re.findall(domain, modified)]
+ modified = re.sub(email, "{}", modified). \
+ format(*host)
+ else:
+ modified = re.sub(email, "[EMAIL]", modified)
+ return modified
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, text):
+ return bool(re.match(email, text))
+
+ @classmethod
+ def is_phone(cls, text):
+ return bool(re.match(phone, text))
+
+ @classmethod
+ def is_hostname(cls, text):
+ return bool(re.match(domain_no_at, text))
+
+ @classmethod
+ def is_ip_address(cls, text):
+ return bool(re.match(ip, text))
+
+ @classmethod
+ def is_number(cls, text):
+ return bool(re.match(number, text))
+
+ @classmethod
+ def is_integer(cls, text):
+ return bool(re.match(integer, text))
+
+ @classmethod
+ def is_date(cls, text):
+ return bool(re.match(date, text))
+
+ @classmethod
+ def is_time(cls, text):
+ return bool(re.match(time, text))
+
+ @classmethod
+ def is_datetime(cls, text):
+ return (bool(re.match(datetime, text)))