Антонио обнови решението на 21.04.2014 20:05 (преди над 10 години)
+import re
+
+
+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):
+ prefix = r'((?<=\s)|^)((00|\+)[1-9][0-9]{0,2}|0(?!0))'
+ number = r'(( |-|\(|\)){0,2}?\d){6,11}?((?= \D)|(?= \D)|(?=\s)|$)'
+ user = r'((?<=\s)|^)[a-zA-Z0-9][\w\+\.-]{,200}@'
+ domain = r'(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
+ TLD = r'[a-zA-Z]{2,3}(?=\s|$)'
+ filter = '[FILTERED]'
+ filter_code = ' [FILTERED]'
+ text = self._text
+
+ if self.partially_preserve_email_username:
+ name1 = r'((?<=^[a-zA-Z0-9][\w\+\.-]{2})|'
+ name2= r'(?<=\s[a-zA-Z0-9][\w\+\.-]{2}))[\w\+\.-]{3,198}(?=@'
+ text = re.sub(name1 + name2 + domain + TLD + ')', filter, text)
+ self.preserve_email_hostname = True
+ if self.preserve_email_hostname:
+ name = r'((?<=\s)|^)[a-zA-Z0-9][\w\+\.-]{,200}(?=@'
+ text = re.sub(name + domain + TLD + ')', filter, text)
+
+ text = re.sub(user + domain + TLD, '[EMAIL]', text)
+
+ if self.preserve_phone_country_code:
+ code_space = r'((?<=\s00[1-9]\d\d)|(?<=\s\+[1-9]\d\d)|'
+ code_start = r'(?<=^00[1-9]\d\d)|(?<=^\+[1-9]\d\d))'
+ text = re.sub(code_space + code_start + number , filter_code, text)
+ code_space = r'((?<=\s00[1-9]\d)|(?<=\s\+[1-9]\d)|'
+ code_start = r'(?<=^00[1-9]\d)|(?<=^\+[1-9]\d))'
+ text = re.sub(code_space + code_start + number , filter_code, text)
+ code_space = r'((?<=\s00[1-9])|(?<=\s\+[1-9])|'
+ code_start = r'(?<=^00[1-9])|(?<=^\+[1-9]))'
+ text = re.sub(code_space + code_start + number , filter_code, text)
+
+ text = re.sub(prefix + number, '[PHONE]', text)
+
+ return text
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, value):
+ user = r'^[a-zA-Z0-9][\w\+\.-]{,200}@'
+ domain = r'(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
+ TLD = r'[a-zA-Z]{2,3}$'
+
+ return re.search(user + domain + TLD, value) != None
+
+ @classmethod
+ def is_phone(cls, value):
+ prefix = r'^((00|\+)[1-9][0-9]{0,2}|0(?!0))'
+ number = r'(( |-|\(|\)){0,2}?\d){6,11}?$'
+
+ return re.search(prefix + number, value) != None
+
+ @classmethod
+ def is_hostname(cls, value):
+ domain = r'^(([a-zA-Z\d][a-zA-Z\d-]{,61}[a-zA-Z\d]|[a-zA-Z\d])\.)+'
+ TLD = r'[a-zA-Z]{2,3}$'
+
+ return re.search(domain + TLD, value) != None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ ip = r'^((\d|\d\d|1\d\d|2[0-4]\d|25[0-5])\.){3}'
+ ip_last_number = r'(\d|\d\d|1\d\d|2[0-4]\d|25[0-5])$'
+
+ return re.search(ip + ip_last_number, value) != None
+
+ @classmethod
+ def is_number(cls, value):
+ number = r'^-{0,1}(0|[1-9]\d*)(\.\d+){0,1}$'
+
+ return re.search(number, value) != None
+
+ @classmethod
+ def is_integer(cls, value):
+ number = r'^-{0,1}(0|[1-9]\d*)$'
+
+ return re.search(number, value) != None
+
+ @classmethod
+ def is_date(cls, value):
+ date = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])$'
+
+ return re.search(date, value) != None
+
+ @classmethod
+ def is_time(cls, value):
+ time = r'^([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$'
+
+ return re.search(time, value) != None
+
+ @classmethod
+ def is_datetime(cls,value):
+ date = r'^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2]\d|3[0-1])( |T)'
+ time = r'([0-1]\d|2[0-3]):[0-5]\d:[0-5]\d$'
+
+ return re.search(date + time, value) != None