Любомир обнови решението на 23.04.2014 13:54 (преди над 10 години)
+import re
+
+
+class Validations:
+ ip_regex = r'(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'
+
+ int_regex = r'-?([1-9]\d*|0)'
+ fraction_regex = r'-?\d+\.\d+'
+
+ date_regex = r'\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2]\d|3[0-1])'
+ time_regex = r'(?:2[0-3]|[0-1]\d):[0-5][0-9]:[0-5][0-9]'
+ datetime_regex = date_regex + r'(?:T| |\b)' + time_regex
+
+ local_phone_regex = r'0[1-9]*\d*(?:[ -\(\)]{,2}\d){6,11}'
+ international_phone_regex = r'(?:00|\+)[1-9]\d{,2}(?:[ -\(\)]{,2}\d){6,11}'
+
+ host_regex = r'(?:(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+(?:[a-zA-Z]{2}|[a-zA-Z]{3}|[a-zA-Z]{2}\.[a-zA-Z]{2}|[a-zA-Z]{3}\.[a-zA-Z]{2})'
+ email_name_regex = r'(?:[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9_+\.\-]{,199})'
+
+ @staticmethod
+ def _format_regex(*args):
+ return '^' + ''.join(args) + '$'
+
+ @classmethod
+ def is_email(cls, value):
+ return bool(re.match(
+ cls._format_regex(cls.email_name_regex, '@', cls.host_regex),
+ value
+ ))
+
+ @classmethod
+ def is_phone(cls, value):
+ return bool(
+ re.match(cls._format_regex(cls.local_phone_regex), value) or
+ re.match(cls._format_regex(cls.international_phone_regex), value)
+ )
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(re.match(cls._format_regex(cls.host_regex), value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(re.match(cls._format_regex(cls.ip_regex), value))
+
+ @classmethod
+ def is_number(cls, value):
+ return bool(
+ cls.is_integer(value) or
+ re.match(cls._format_regex(cls.fraction_regex), value)
+ )
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(re.match(cls._format_regex(cls.int_regex), value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(re.match(cls._format_regex(cls.date_regex), value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(re.match(cls._format_regex(cls.time_regex), value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ return bool(re.match(cls._format_regex(cls.datetime_regex), value))
+
+
+class PrivacyFilter:
+ filter_string = '[FILTERED]'
+ email_string = '[EMAIL]'
+ phone_string = '[PHONE]'
+
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ mail_regex = r'\b({})({})\b'.format(
+ Validations.email_name_regex, '@' + Validations.host_regex
+ )
+
+ local_phone_regex = r'\b({})\b'.format(Validations.local_phone_regex)
+
+ international_phone_regex = '\b({})\b'.format(
+ Validations.international_phone_regex
+ )
+
+ def __init__(self, text):
+ self.unfiltered = text
+
+ def filtered(self):
+ return self.filter_phone(self.filter_mail(self.unfiltered))
+
+ def filter_mail(self, text):
+ return re.sub(self.mail_regex, self.mail_replace, text)
+
+ def mail_replace(self, match_object):
+ if self.partially_preserve_email_username:
+ name = self.filter_string
+ if len(match_object.group(1)) >= 6:
+ name = match_object.group(1)[0:3] + name
+ return name + match_object.group(2)
+
+ elif self.preserve_email_hostname:
+ return self.filter_string + match_object.group(2)
+
+ else:
+ return self.email_string
+
+
+ def filter_phone(self, text):
+ return re.sub(
+ self.international_phone_regex,
+ self.phone_replace,
+ re.sub(self.local_phone_regex, self.phone_string, text)
+ )
+
+ def phone_replace(self, match_object):
+ if not self.preserve_phone_country_code:
+ return self.phone_string
+
+ return self.phone_string
+