Веселин обнови решението на 21.04.2014 01:08 (преди над 10 години)
+import re
+
+
+HOSTNAME = (r'^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{,60}'
+ r'[a-zA-Z0-9])\.)+([A-Za-z]{2,3}(\.[A-Za-z]{2})?)$')
+USERNAME = r'[a-zA-Z0-9][a-zA-Z0-9_+.-]{,200}'
+EMAIL = r'^{}@{}$'.format(USERNAME, HOSTNAME[1:-1])
+LOCAL_PHONE = r'^0[-() ]?[1-9]([-() ]?[0-9]){5,10}$'
+INTERNATIONAL_PHONE_PREFIX = r'((00)|\+)[1-9][0-9]{,2}'
+INTERNATIONAL_PHONE_SUFFIX = r'[-() ]{,2}?[1-9]([-() ]{,2}?[0-9]){5,10}'
+INTERNATIONAL_PHONE = r'^{}{}$'.format(INTERNATIONAL_PHONE_PREFIX,
+ INTERNATIONAL_PHONE_SUFFIX)
+IP_ADDRESS = (r'^(([0-9]|[1-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}'
+ r'([0-9]|[1-9][0-9]|2[0-4][0-9]|25[0-5])$')
+NUMBER = r'^\-?(0|[1-9][0-9]*)\.?[0-9]+$'
+INTEGER = r'^\-?(0|[1-9][0-9]*)$'
+DATE = r'^[0-9]{4}\-(0[1-9]|1[0-2])\-(0[1-9]|[12][0-9]|3[0-1])$'
+TIME = r'^([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
+DATETIME = r'^{}[ T]{}$'.format(DATE[1:-1], TIME[1:-1])
+
+
+def matches(pattern, value):
+ return re.match(pattern, value) is not None
+
+
+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):
+ text = re.sub(LOCAL_PHONE[1:-1], '[PHONE]', self.__text)
+ filter_phone = lambda phone: self.filter_phone(phone.group())
+ text = re.sub(INTERNATIONAL_PHONE[1:-1], filter_phone, text)
+ filter_email = lambda email: self.filter_email(email.group())
+ text = re.sub(EMAIL[1:-1], filter_email, text)
+ return text
+
+ def filter_phone(self, phone):
+ if ((re.match(INTERNATIONAL_PHONE, phone) and
+ self.preserve_phone_country_code)):
+ return (re.match(INTERNATIONAL_PHONE_PREFIX, phone).group()
+ + ' [FILTERED]')
+ else:
+ return '[PHONE]'
+
+ def filter_email(self, email):
+ if email.index('@') >= 6 and self.partially_preserve_email_username:
+ filter_username = lambda match: match.group()[0:3] + '[FILTERED]'
+ return re.sub(USERNAME, filter_username, email, 1)
+ elif (self.partially_preserve_email_username or
+ self.preserve_email_hostname):
+ return re.sub(USERNAME, '[FILTERED]', email, 1)
+ else:
+ return '[EMAIL]'
+
+
+class Validations:
+ @classmethod
+ def is_hostname(cls, value):
+ return matches(HOSTNAME, value)
+
+ @classmethod
+ def is_email(cls, value):
+ return matches(EMAIL, value)
+
+ @classmethod
+ def is_phone(cls, value):
+ return (matches(LOCAL_PHONE, value) or
+ matches(INTERNATIONAL_PHONE, value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return matches(IP_ADDRESS, value)
+
+ @classmethod
+ def is_number(cls, value):
+ return matches(NUMBER, value)
+
+ @classmethod
+ def is_integer(cls, value):
+ return matches(INTEGER, value)
+
+ @classmethod
+ def is_date(cls, value):
+ return matches(DATE, value)
+
+ @classmethod
+ def is_time(cls, value):
+ return matches(TIME, value)
+
+ @classmethod
+ def is_datetime(cls, value):
+ return matches(DATETIME, value)
Решението ти ми харесвса.
- Опитай се да намериш число, което твоят израз не хваща. Няма да ти отнеме много време :)
- Използвай
\b
за разни неща