Мария обнови решението на 19.04.2014 17:46 (преди над 10 години)
+import re
+
+
+def matcher(regex, string):
+ matches = re.match(regex, string)
+ if matches is None:
+ return False
+ return True
+
+
+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):
+ filtered_lbl = '[FILTERED]'
+ email_lbl = '[EMAIL]'
+ phone_lbl = '[PHONE]'
+ username = r'([a-zA-Z\d][+a-zA-Z0-9-_\.]{,200})'
+ tld = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
+ domainname = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
+ email = username + '@(' + domainname + r'\.)+' + tld
+ username_email = username + \
+ r'(?=@(' + domainname + r'\.)' + tld + r')'
+ phone = r'(0(?!0)|((00|\+)[1-9]\d{0,2}))([ )\(\-]{,2}\d){6,11}'
+
+ result = self.text
+
+ if self.partially_preserve_email_username:
+ def f(match):
+ if(len(match.group())) < 6:
+ return filtered_lbl
+ return match.group()[:3] + filtered_lbl
+ result = re.sub(username_email, f, result)
+ elif self.preserve_email_hostname:
+ result = re.sub(username_email, filtered_lbl, result)
+ else:
+ result = re.sub(email, email_lbl, result)
+ if self.preserve_phone_country_code:
+ result = re.sub(phone,
+ lambda match: match.group(2) + ' ' + filtered_lbl,
+ result)
+ result = re.sub(phone, phone_lbl, result)
+ return result
+
+
+class Validations:
+
+ @classmethod
+ def is_email(cls, value):
+ username = r'([a-zA-Z\d][+a-zA-Z0-9-_\.]{,200})'
+ tld = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
+ domainname = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
+ email = username + '@(' + domainname + r'\.)+' + tld + '$'
+ return matcher(email, value)
+
+ @classmethod
+ def is_phone(cls, value):
+ phone = r'(0(?!0)|((00|\+)[1-9]\d{0,2}))([ )\(\-]{,2}\d){6,11}$'
+ return matcher(phone, value)
+
+ @classmethod
+ def is_hostname(cls, value):
+ tld = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
+ domainname = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
+ hostname = '(' + domainname + r'\.)+' + tld + '$'
+ return matcher(hostname, value)
+
+ @classmethod
+ def is_ip_address(cls, value):
+ bytes = r'(\d(?!\d)|[1-9]\d(?!\d)|1\d{2}|2([0-4]\d|5[0-5]))'
+ ip = bytes + r'\.' + bytes + r'\.' + bytes + r'\.' + bytes + '$'
+ return matcher(ip, value)
+
+ @classmethod
+ def is_number(cls, value):
+ double = r'-?(0|([1-9]\d*))(\.\d+)?$'
+ return matcher(double, value)
+
+ @classmethod
+ def is_integer(cls, value):
+ integer = r'-?(0|([1-9]\d*))$'
+ return matcher(integer, value)
+
+ @classmethod
+ def is_date(cls, value):
+ date = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1\d|3[01])$'
+ return matcher(date, value)
+
+ @classmethod
+ def is_time(cls, value):
+ time = r'([01]\d|2[0-3]):[0-5][0-9]:[0-5][0-9]$'
+ return matcher(time, value)
+
+ @classmethod
+ def is_datetime(cls, value):
+ date = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1\d|3[01])'
+ time = r'([01]\d|2[0-3]):[0-5][0-9]:[0-5][0-9]'
+ datetime = date + r'[ T]' + time + r'$'
+ return matcher(datetime, value)
- Можеш да изнесеш изразите в константи, част от тях използваш на повече от едно място
- Виж къде
\b
би ти било полезно - Какво ще се случи тук
match.group(2) + ' ' + filtered_lbl
, ако няма втора група?