Ангел обнови решението на 23.04.2014 16:27 (преди над 10 години)
+import re
+PHONE = r'''(?P<prefix>((?<![0-9a-zA-Z+])0(?!0)|(00|\+)[1-9][0-9]{0,2}))
+ (?P<payload>([()\- ]{0,2}[0-9]){5,10}[0-9])'''
+IP = r'''\b([0]|[1-9]\d?|[1]\d{2}|2([0-4]\d|5[0-5]))
+ \.([0]|[1-9]\d?|[1]\d{2}|2([0-4]\d|5[0-5]))
+ \.([0]|[1-9]\d?|[1]\d{2}|2([0-4]\d|5[0-5]))
+ \.([0]|[1-9]\d?|[1]\d{2}|2([0-4]\d|5[0-5]))\b'''
+HOSTNAME = r'''(([a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]|[a-zA-Z0-9])\.)+
+ ([a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)'''
+EMAIL = r'([a-zA-Z0-9][a-zA-Z0-9_+.\-]{0,200})(?P<hostname>@' \
+ + HOSTNAME + r')'
+EMAIL_LONG = r'''(?P<shortuser>[a-zA-Z0-9][a-zA-Z0-9_+.\-]{2})
+ (?P<longuser>[a-zA-Z0-9_+.\-]{3,194})(?P<hostname>@''' \
+ + HOSTNAME + r')'
+DATE = r'([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])'
+TIME = r'([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])'
+DATETIME = DATE + r'[ T]' + TIME
+INTEGER = r'-?(0|[1-9][0-9]*)'
+NUMBER = INTEGER + r'(\.[0-9]+)?'
+
+
+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):
+ if self.preserve_phone_country_code:
+ result = re.sub(PHONE, r'\g<prefix> [FILTERED]',
+ self._text, flags=re.X)
+ else:
+ result = re.sub(PHONE, r'[PHONE]', self._text, flags=re.X)
+
+ if self.partially_preserve_email_username:
+ result = re.sub(EMAIL_LONG,
+ r'\g<shortuser>[FILTERED]\g<hostname>',
+ result, flags=re.X)
+
+ if (self.preserve_email_hostname
+ or self.partially_preserve_email_username):
+ result = re.sub(EMAIL, r'[FILTERED]\g<hostname>',
+ result, flags=re.X)
+ else:
+ result = re.sub(EMAIL, r'[EMAIL]', result, flags=re.X)
+ return result
+
+
+class Validations:
+ @classmethod
+ def checkmatch(cls, regex, value):
+ return re.match(r'^' + regex + r'$', value, re.X) is not None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return cls.checkmatch(IP, value)
+
+ @classmethod
+ def is_hostname(cls, value):
+ return cls.checkmatch(HOSTNAME, value)
+
+ @classmethod
+ def is_email(cls, value):
+ return cls.checkmatch(EMAIL, value)
+
+ @classmethod
+ def is_date(cls, value):
+ return cls.checkmatch(DATE, value)
+
+ @classmethod
+ def is_time(cls, value):
+ return cls.checkmatch(TIME, value)
+
+ @classmethod
+ def is_datetime(cls, value):
+ return cls.checkmatch(DATETIME, value)
+
+ @classmethod
+ def is_integer(cls, value):
+ return cls.checkmatch(INTEGER, value)
+
+ @classmethod
+ def is_number(cls, value):
+ return cls.checkmatch(NUMBER, value)
+
+ @classmethod
+ def is_phone(cls, value):
+ return cls.checkmatch(PHONE, value)