Никола обнови решението на 19.04.2014 03:46 (преди над 10 години)
+import re
+import functools
+
+PATTERNS = {
+ "HOSTNAME": r"(?P<hostname>\b([0-9A-Za-z]([0-9A-Za-z]|[0-9A-Za-z\-]{1,61}[0-9A-Za-z])\.)" + # (Sub)domains
+ r"+([A-Za-z]{2,3}(\.[A-Za-z]{2})?)\b)", # TLD
+ "EMAIL_ACCOUNT": r"(?P<account>\b[0-9A-Za-z][0-9A-Za-z_\-\.\+]{0,200})\b",
+ "BYTE": r"(\b0|[1-9][0-9]?|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\b",
+ "DATE": r"\b[0-9]{4}-(0[1-9]|1[0-2])-([1-9]|[1-2][0-9]|3[01])\b",
+ "TIME": r"\b([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\b",
+ "FLOAT": r"-?(0|[1-9]\d*)(\.\d+)?\b",
+ "INT": r"-?(0|[1-9]\d*)",
+ "PHONE": r"((?P<l_prefix>0(?!0))|(?P<g_prefix>(00|\+)[1-9][0-9]{0,2}))(?P<main>([ \(\)\-]{0,2}\d){6,11})"
+ }
+
+PATTERNS["EMAIL"] = PATTERNS["EMAIL_ACCOUNT"] + "@" + PATTERNS["HOSTNAME"]
+# An IP is 4 bytes
+PATTERNS["IP"] = r"\b" + r"\.".join([PATTERNS["BYTE"]] * 4) + r"\b"
+# Remove the requirements for word break
+PATTERNS["DATETIME"] = PATTERNS["DATE"][:-2] + r"( |T)" + PATTERNS["TIME"][:2]
+
+class Validations:
+
+ def __validate(patternName, string):
+ return bool(re.match("^{0}$".format(PATTERNS[patternName]), string))
+
+ is_email = functools.partial(__validate, "EMAIL")
+ is_phone = functools.partial(__validate, "PHONE")
+ is_hostname = functools.partial(__validate, "HOSTNAME")
+ is_ip_address = functools.partial(__validate, "IP")
+ is_number = functools.partial(__validate, "FLOAT")
+ is_integer = functools.partial(__validate, "INT")
+ is_date = functools.partial(__validate, "DATE")
+ is_time = functools.partial(__validate, "TIME")
+ is_datetime = functools.partial(__validate, "DATETIME")
+
+
+class PrivacyFilter:
+ EMAIL_PLACEHOLDER = "[EMAIL]"
+ PHONE_PLACEHOLDER = "[PHONE]"
+ FILTERED_PLACEHOLDER = "[FILTERED]"
+ MIN_USER_LENGTH = 6
+
+ 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 __phone_filter(self, match):
+ if self.preserve_phone_country_code and match.group("g_prefix"):
+ return "{0} {1}".format(match.group("g_prefix"),
+ PrivacyFilter.FILTERED_PLACEHOLDER)
+ return PrivacyFilter.PHONE_PLACEHOLDER
+
+ def __email_filter(self, match):
+ if self.partially_preserve_email_username:
+ user = match.group("account")
+ user = user[0:3] * (len(user) >= PrivacyFilter.MIN_USER_LENGTH)
+ return "{0}{1}@{2}".format(user,
+ PrivacyFilter.FILTERED_PLACEHOLDER,
+ match.group("hostname"))
+
+ if self.preserve_email_hostname:
+ return "{0}@{1}".format(PrivacyFilter.FILTERED_PLACEHOLDER,
+ match.group("hostname"))
+ return PrivacyFilter.EMAIL_PLACEHOLDER
+
+ def filtered(self):
+ filtered = re.sub(PATTERNS["EMAIL"], self.__email_filter, self.text)
+ filtered = re.sub(PATTERNS["PHONE"], self.__phone_filter, filtered)
+ return filtered
- Използвай
\b
, където е необходимо - Датата трябва да е във формат yyyy-mm-dd. Едноцифрените дати трябва да започват с 0
- Провери си шаблона
datetime