Васил обнови решението на 21.04.2014 00:38 (преди над 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):
+ newText = self.text
+ pattern_dom = r'(\w(([\w-]{0,61}?)\w)?\.)'
+ pattern_tld = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?\b'
+ pattern_hostname = pattern_dom + r'+' + pattern_tld + r'\b'
+ pattern_email = r'\b[\w][\w_\-\.\+]{0,200}@'
+ pattern_email += '(' + pattern_hostname + ')'
+ phone = r'(((\+|00)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){5,10}'
+ if not self.preserve_email_hostname and not self.partially_preserve_email_username:
+ newText = re.sub(pattern_email, '[EMAIL]', newText)
+ elif self.preserve_email_hostname and not self.partially_preserve_email_username:
+ newText = re.sub(pattern_email, '[FILTERED]' + '@' + '\\1', newText)
+ else:
+ pattern_short_email = r'\b[\w][\w_\-\.\+]{0,5}@'
+ pattern_short_email += '(' + pattern_hostname + ')'
+ newText = re.sub(pattern_short_email, '[FILTERED]@' + '\\1', newText)
+ pattern_long_mail = r'\b([\w][\w_\-\.\+]{2})[\w_\-\.\+]+@'
+ pattern_long_mail += r'(' + pattern_hostname + ')'
+ newText = re.sub(pattern_long_mail, '\\1' + '[FILTERED]@' + '\\2', newText)
+
+ if not self.preserve_phone_country_code:
+ newText = re.sub(phone, '[PHONE]', newText)
+ else:
+ international = r'((\+|00)[1-9]\d{0,2})[-\(\)\s]{0,2}[1-9]([-\(\)\s]{0,2}\d){5,10}\b'
+ newText = re.sub(international, '\\1' + ' [FILTERED]', newText)
+ country = r'\b0([-\(\)\s]{0,2}\d){6,11}\b'
+ newText = re.sub(country, '[FILTERED]', newText)
+ return newText
+
+
+class Validations:
+ def is_email(value):
+ pattern_dom = r'(\w(([\w-]{0,61}?)\w)?\.)'
+ pattern_tld = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?'
+ pattern_hostname = pattern_dom + r'+' + pattern_tld + r'$'
+ pattern = r'^[\w][\w_\-\.\+]{0,200}@'
+ pattern += pattern_hostname
+ return(bool(re.match(pattern, value)))
+
+ def is_phone(value):
+ pattern = r'^(((\+|00)[1-9]\d{0,2})|0)[-\(\)\s]{0,2}[1-9]'
+ pattern += r'([-\(\)\s]{0,2}\d){5,10}$'
+ return bool(re.match(pattern, value))
+
+ def is_hostname(value):
+ pattern_dom = r'(\w(([\w-]{0,61}?)\w)?\.)'
+ pattern_tld = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?'
+ pattern = r'^' + pattern_dom + r'+' + pattern_tld + r'$'
+ return bool(re.match(pattern, value))
+
+ def is_ip_address(value):
+ matchNum = r'(1?[1-9]?\d|2[0-4]\d|25[0-5])'
+ pattern = r'^' + (matchNum + '\.') * 3 + matchNum + '$'
+ return bool(re.match(pattern, value))
+
+ def is_number(value):
+ pattern = r'^-?(0|[1-9]\d*)([\.,]\d+)?$'
+ return bool(re.match(pattern, value))
+
+ def is_integer(value):
+ pattern = r'^-?(0|[1-9]\d*)$'
+ return bool(re.match(pattern, value))
+
+ def is_date(value):
+ pattern = r'^\d{4}-(02-([01]\d|2[1-8])|(01|03|05|07|08|10|12)'
+ pattern += r'-([012]\d|3[01])|(04|06|09|11)-([012]\d|30))$'
+ return bool(re.match(pattern, value))
+
+ def is_time(value):
+ hour_patt = r'(2[0-3]|[01]\d|)'
+ min_patt = r'[0-5]\d'
+ pattern = r'^' + hour_patt + ":" + min_patt + ":" + min_patt + '$'
+ return bool(re.match(pattern, value))
+
+ def is_datetime(value):
+ date_pattern = r'^\d{4}-(02-([01]\d|2[1-8])|(01|03|05|07|08|10|12)'
+ date_pattern += r'-([012]\d|3[01])|(04|06|09|11)-([012]\d|30))'
+ hour_patt = r'(2[0-3]|[01]\d|)'
+ min_patt = r'[0-5]\d'
+ time_pattern = hour_patt + ":" + min_patt + ":" + min_patt + '$'
+ return bool(re.match(date_pattern + r'\s' + time_pattern, value))
- Именуваш отвратително разни неща, в някои случаи дори в "camelCase"
- Погледни какво хваща регулярният израз r
\b
- Опрости кода на метода
filtered
.