Мариан обнови решението на 23.04.2014 15:54 (преди над 10 години)
+import re
+
+
+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):
+ result = self.text
+ if self.partially_preserve_email_username:
+ email_pattern = re.compile(r"""\b([a-zA-Z\d][a-zA-Z\d_+.-]{,2})
+ [a-zA-Z\d_+.-]{,198}@
+ ((([a-zA-Z\d][a-zA-Z\d-]{,62})\.)*[a-zA-Z]{2,3}
+ (\.[a-zA-Z]{,2})?)\b""", re.X)
+ result = re.sub(email_pattern, '\g<1>[FILTERED]@\g<2>', result)
+ elif self.preserve_email_hostname:
+ email_pattern = re.compile(r"""\b[a-zA-Z\d][a-zA-Z\d_+.-]{,200}@
+ ((([a-zA-Z\d][a-zA-Z\d-]{,62})\.)*[a-zA-Z]{2,3}
+ (\.[a-zA-Z]{,2})?)\b""", re.X)
+ result = re.sub(email_pattern, '[FILTERED]@\g<1>', result)
+ else:
+ email_pattern = re.compile(r"""\b[a-zA-Z\d][a-zA-Z\d_+.-]{,200}@
+ (([a-zA-Z\d][a-zA-Z\d-]{,62})\.)*[a-zA-Z]{2,3}
+ (\.[a-zA-Z]{,2})?\b""", re.X)
+ result = re.sub(email_pattern, '[EMAIL]', result)
+ if self.preserve_phone_country_code:
+ phone_pattern = re.compile(r"""((\b00|\+)[^0][\d]{,2})
+ [-\ \(\)\d]([-\ \(\)]{,2}\d){5,10}\b""", re.X)
+ result = re.sub(phone_pattern, '\g<1> [FILTERED]', result)
+ phone_pattern = re.compile(r"""\b0(?!0)
+ [-\ \(\)\d]([-\ \(\)]{,2}\d){5,10}\b""", re.X)
+ result = re.sub(phone_pattern, '[PHONE]', result)
+ else:
+ phone_pattern = re.compile(r"""\b0(?!0)
+ [-\ \(\)\d](?:[-\ \(\)]{,2}\d){4,9}
+ |
+ ((\b00|\+)[^0][\d]{,2})
+ [-\ \(\)\d]([-\ \(\)]{,2}\d){5,10}\b""", re.X)
+ result = re.sub(phone_pattern, '[PHONE]', result)
+ return result
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, email):
+ filter = PrivacyFilter(email)
+ if filter.filtered() == '[EMAIL]':
+ return True
+ return False
+
+ @classmethod
+ def is_phone(cls, phone):
+ filter = PrivacyFilter(phone)
+ if filter.filtered() == '[PHONE]':
+ return True
+ return False
+
+ @classmethod
+ def is_hostname(cls, hostname):
+ hostname_pattern = re.compile(r"""^([a-zA-Z\d]([a-zA-Z\d-]){,62}\.)*
+ [a-zA-Z]{2,3}(\.[a-zA-Z]{,2})?$""", re.X)
+ check = re.match(hostname_pattern, hostname)
+ if check != None:
+ return True
+ return False
+
+ @classmethod
+ def is_ip_address(cls, address):
+ ip_pattern = r'^\d+\.\d+\.\d+\.\d+$'
+ check = re.match(ip_pattern, address)
+ if check != None:
+ print(check.group())
+ bytes = address.split('.')
+ print(bytes)
+ for number in bytes:
+ if int(number) > 255:
+ return False
+ return True
+ return False
+
+ @classmethod
+ def is_number(cls, number):
+ number_pattern = r'^-?(0|[1-9][0-9]*)(\.[0-9]+)?$'
+ check = re.match(number_pattern, number)
+ if check != None:
+ return True
+ return False
+
+ @classmethod
+ def is_integer(cls, integer):
+ integer_pattern = r'^-?(0|[1-9][0-9]*)$'
+ check = re.match(integer_pattern, integer)
+ if check != None:
+ return True
+ return False
+
+ @classmethod
+ def is_date(cls, date):
+ date_pattern = r'^\d{4,4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])$'
+ check = re.match(date_pattern, date)
+ if check != None:
+ return True
+ return False
+
+ @classmethod
+ def is_time(cls, time):
+ time_pattern = r'^([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$'
+ check = re.match(time_pattern, time)
+ if check != None:
+ return True
+ return False
+
+ @classmethod
+ def is_datetime(cls, datetime):
+ patt = re.compile(r"""^\d{4,4}-(0[1-9]|1[0-2])-([0-2][0-9]|3[0-1])
+ [T\ ]
+ ([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$""", re.X)
+ check = re.match(patt, datetime)
+ if check != None:
+ return True
+ return False