Атанас обнови решението на 19.04.2014 15:07 (преди над 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):
+ email_name = r'\b\w[\w\+\-_\.]{,200}@'
+ hostname = r'(\w[\w\-]{0,61}\w\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?\b'
+ email_pattern = email_name + hostname
+ filtered_text = self.text
+ while re.search(email_pattern, filtered_text):
+ email = re.search(email_pattern, filtered_text).group()
+ pivot = email.index('@')
+ if self.partially_preserve_email_username:
+ if pivot >= 6:
+ change = email[:3] + '[FILTERED]' + email[pivot:]
+ else:
+ change = '[FILTERED]' + email[pivot:]
+ elif self.preserve_email_hostname:
+ change = '[FILTERED]' + email[pivot:]
+ else:
+ change = '[EMAIL]'
+ filtered_text = filtered_text.replace(email, change, 1)
+
+ local = r'\b(0[ \-()]{,2}[1-9]|0[ \-()]{1,2}\d)([ \-()]{,2}\d){5,10}\b'
+ international = r'((00|\+)[1-9]\d{0,2})([ \-()]{,2}\d){6,11}\b'
+ phone_pattern = local + r'|' + international
+ while re.search(phone_pattern, filtered_text):
+ phone = re.search(phone_pattern, filtered_text).group()
+ if Validations.is_local_phone(phone):
+ change = '[PHONE]'
+ elif self.preserve_phone_country_code:
+ change = re.search(international, phone).group(1) + \
+ ' [FILTERED]'
+ else:
+ change = '[PHONE]'
+ filtered_text = filtered_text.replace(phone, change, 1)
+ return filtered_text
+
+
+class Validations():
+ def is_email(value):
+ pattern = r'\w[\w\+\-_\.]{,200}@'
+ start_of_hostname = value.index('@') + 1
+ hostname = value[start_of_hostname:]
+ return bool(re.match(pattern, value)) and \
+ Validations.is_hostname(hostname)
+
+ def is_phone(value):
+ return Validations.is_local_phone(value) or \
+ Validations.is_international_phone(value)
+
+ def is_local_phone(value):
+ local = r'(0[ \-()]{,2}[1-9]|0[ \-()]{1,2}\d)([ \-()]{,2}\d){5,10}$'
+ return bool(re.match(local, value))
+
+ def is_international_phone(value):
+ international = r'(00|\+)[1-9]\d{0,2}([ \-()]{,2}\d){6,11}$'
+ return bool(re.match(international, value))
+
+ def is_hostname(value):
+ pattern = r'(\w[\w\-]{0,61}\w\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
+ return bool(re.match(pattern, value))
+
+ def is_ip_address(value):
+ if re.match(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', value):
+ for number in value.split('.'):
+ if int(number) >= 256:
+ return False
+ return True
+ return False
+
+ def is_number(value):
+ pattern = r'0$|-0$|0\.\d+$|-0\.\d+$|[1-9]\d*\.\d+$|-[1-9]\d*\.\d+$|[1-9]\d*$'
+ return bool(re.match(pattern, value))
+
+ def is_integer(value):
+ return bool(re.match(r'0$|-0$|[1-9]\d*$|-[1-9]\d*$', value))
+
+ def is_date(value):
+ date = value.split('-')
+ return bool(re.match(r'\d{4}-\d{2}-\d{2}$', value)) and \
+ int(date[1]) >= 1 and int(date[1]) <= 12 and \
+ int(date[2]) >= 1 and int(date[2]) <= 31
+
+ def is_time(value):
+ time = value.split(':')
+ return bool(re.match(r'\d{2}:\d{2}:\d{2}$', value)) and \
+ int(time[0]) >= 0 and int(time[0]) <= 23 and \
+ int(time[1]) >= 0 and int(time[1]) <= 59 and \
+ int(time[2]) >= 0 and int(time[2]) <= 59
+
+ def is_datetime(value):
+ pattern = r'\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}$'
+ return bool(re.match(pattern, value))
Използвай \b
, където е необходимо. Виж какви валидации ти липсват в datetime
, а ги имаш в date