Константин обнови решението на 23.04.2014 13:43 (преди над 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 filter_email(self, txt):
+ if self.partially_preserve_email_username == True:
+ self.preserve_email_hostname = True
+ founded = re.findall(r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@', txt)
+ for f in founded:
+ f = re.split(r'.{3}', f, 1)
+ txt = re.sub(f[1], '[FILTERED]@', txt)
+
+ self.text = txt
+ elif self.preserve_email_hostname == True:
+ self.text = re.sub(
+ r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@', '[FILTERED]@', txt)
+ else:
+ self.text = re.sub(
+ r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@'
+ +
+ r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+[a-zA-Z]{2,3}'
+ + r'(\.[a-zA-Z]{2,3})?', '[EMAIL]', txt)
+
+ def filter_phone(self, txt):
+ if self.preserve_phone_country_code == True:
+ txt = re.sub(
+ r'[^a-zA-Z0-9\+]0( |-|\(|\)){,2}[1-9]'
+ + r'(( |-|\(|\)){,2}\d){5,10}', ' [PHONE]', txt)
+
+ founded = re.findall(
+ r'((\b00|\+)[1-9]\d{,2}(( |-|\(|\)){,2}\d){6,11})', txt)
+ for f in founded:
+ f = re.split(r'((\b00|\+)[1-9]\d{,2})', f[0], 1)
+ txt = re.sub(f[len(f) - 1], ' [FILTERED]', txt)
+
+ self.text = txt
+ else:
+ self.text = re.sub(
+ r'([^a-zA-Z0-9\+]0|\b00|\+)[1-9]\d{,2}'
+ + r'(( |-|\(|\)){,2}\d){6,11}', ' [PHONE]', txt)
+
+ def filtered(self):
+ self.filter_email(self.text)
+ self.filter_phone(self.text)
+ return self.text
+
+
+class Validations:
+
+ def is_email(value):
+ matched = re.match(
+ r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@'
+ + r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+[a-zA-Z]{2,3}'
+ + r'(\.[a-zA-Z]{2,3})?$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_phone(value):
+ matched = re.match(
+ r'([^a-zA-Z0-9\+]0|\b00|\+)[1-9]\d{,2}'
+ + r'(( |-|\(|\)){,2}\d){6,11}$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_hostname(value):
+ matched = re.match(
+ r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+'
+ + r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_ip_address(value):
+ matched = re.match(
+ r'((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}'
+ + r'(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_number(value):
+ matched = re.match(r'-?(0|[1-9]\d*)(\.\d{1,})?$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_integer(value):
+ matched = re.match(r'-?(0|[1-9]\d*)$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_date(value):
+ matched = re.match(
+ r'\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_time(value):
+ matched = re.match(r'([01]\d|2[0-3]):[0-5]\d:[0-5]\d$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_datetime(value):
+ matched = re.match(
+ r'\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])( |T)'
+ + r'([01]\d|2[0-3]):[0-5]\d:[0-5]\d$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True