Пламен обнови решението на 23.04.2014 13:17 (преди над 10 години)
+import re
+
+
+def check(regex, text):
+ if re.search(regex, text, re.VERBOSE):
+ return True
+ else:
+ return False
+
+
+class Validations(object):
+
+ def is_email(text):
+ regex_email = r'''^(?:[\w][\+\-\_\.\w]*)@
+ (?:[\w][a-zA-Z\-]{0,61}[^W-]\.)+
+ (?:(?:[a-zA-Z]{2}\.)(?:[a-zA-Z]{2}$)|(?:[a-zA-Z]{2,3}$))'''
+ return check(regex_email, text)
+
+ def is_hostname(text):
+ regex_host = r'''^(?:[\w][0-9a-zA-Z\-]{0,62}?\.)+
+ (?:(?:[a-zA-Z]{2}$)|(?:[a-zA-Z]{2}\.[a-zA-Z]{2}$)|
+ (?:[a-zA-Z]{3}$)|([a-zA-Z]{3}\.[a-zA-Z]{2,3}$))'''
+ return check(regex_host, text)
+
+ def is_ip_address(text):
+ regex_ip = r'''^((([01]?[0-9]?[0-9]|[2]?[05]?[05])(\.)){3}
+ ([01]?[0-9]?[0-9]|[2]?[05]?[05]))$'''
+ return check(regex_ip, text)
+
+ def is_number(text):
+ regex = r'^[-]?((?:[1-9][0-9]*\.?[0-9]+)$|(?:[0-9](?:$|\.[0-9]+)))'
+ return check(regex, text)
+
+ def is_integer(text):
+ regex_integer = r'^(?:[-]?[1-9][0-9]*)$|^(?:[-]?[0])$'
+ return check(regex_integer, text)
+
+ def is_date(text):
+ regex_date = r'''^(?:[0-9]{4}-(?:[0][1-9]|[1][0-2])-(?:[0][1-9]|
+ [1-2][0-9]|[3][01]))$'''
+ return check(regex_date, text)
+
+ def is_time(text):
+ regex_time = r'''^(?:([01][0-9]|[2][0-3]):([0-4][0-9]|[5][0-9]):
+ ([0-4][0-9]|[5][0-9]))$'''
+ return check(regex_time, text)
+
+ def is_datetime(text):
+ date_time = re.split(r'[T| ]', text)
+ return Validations.is_date(date_time[0]) and \
+ Validations.is_time(date_time[1])
+
+ def is_phone(text):
+ regex_number = r'''^((?:(?:[0](?!0))|(?:(?:[0]{2}|\+)[1-9][0-9]{0,2})))
+ (?:[\ \-\(\)]{0,2}[0-9]){6,11}$'''
+ return check(regex_number, text)
+
+
+class PrivacyFilter():
+ def __init__(self, text):
+ self.text = text
+ self.filtered_text = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.preserve_email_username = False
+
+ def filter_email(self, start, end):
+ all_regex = r'([^ ]+)@([^ ]+)'
+ self.filtered_text = self.text[:start]+"[EMAIL]"+self.text[end:]
+
+ def filter_username(self):
+ print("in")
+ left_regex = r'([^ ]+)@'
+ left_part = re.search(left_regex, self.text)
+ size_of_left_part = left_part.end() - left_part.start()-1
+ if size_of_left_part > 6:
+ result = self.text[:left_part.start()+3] + '[FILTERED]' + \
+ self.text[left_part.end()-1:]
+ else:
+ result = self.text[:left_part.start()] + '[FILTERED]' + \
+ self.text[left_part.end()-1:]
+ self.filtered_text = result
+
+ def filter_hostname(self):
+ left_regex = r'([^ ]+)@'
+ left_part = re.search(left_regex, self.text)
+ result = self.text[:left_part.start()] + '[FILTERED]' + \
+ self.text[left_part.end()-1:]
+ self.filtered_text = result
+
+ def filtered(self):
+ regex_mail = r'([^ ]+)@([^ ]+)'
+ position = re.search(regex_mail, self.text)
+ if position and Validations.is_email\
+ (self.text[position.start():position.end()]):
+ if self.preserve_email_username is False and \
+ self.preserve_email_hostname is False:
+ self.filter_email(position.start(), position.end())
+ elif self.preserve_email_username:
+ self.filter_username()
+ else:
+ self.filter_hostname()
+
+ regex_phonenumber = r'''((?:(?:[0](?!0))|(?:(?:[0]{2}|\+)
+ [1-9][0-9]{0,2})))(?:[\ \-\(\)]{0,2}[0-9]){6,11}'''
+ position_number = re.search(regex_phonenumber, self.text, re.VERBOSE)
+ if position_number:
+ if Validations.is_phone\
+ (self.text[position_number.start():position_number.end()]):
+ if self.preserve_phone_country_code is False:
+ self.filtered_text = \
+ self.filtered_text[:position_number.start()] + "[PHONE]" +\
+ self.filtered_text[position_number.end():]
+
+ else:
+ compile_ = re.compile(regex_phonenumber, re.VERBOSE)
+ country_code = compile_.search(self.text).groups()
+ size_of_code = len(country_code[0])
+ if size_of_code > 1:
+ self.filtered_text = \
+ self.filtered_text[:position_number.start() + \
+ size_of_code] + " [FILTERED]" + \
+ self.filtered_text[position_number.end():]
+ else:
+ self.filtered_text = \
+ self.filtered_text[:position_number.start()] +\
+ "[PHONE]" + self.filtered_text[position_number.end():]
+
+ return self.filtered_text