Решение на Регулярни изрази от Теодор Халваджиев

Обратно към всички решения

Към профила на Теодор Халваджиев

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 31 успешни тест(а)
  • 8 неуспешни тест(а)

Код

import re
phone = (r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)|"
r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
phone_validation = (r"((((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})"
r"(?=\b)|(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11})))$")
local = r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)"
inter = (r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
country_code = r"(?:(?:(?<=\b)(?<!\+)(?:0{2}))|(?:\+))(?!0)(?:\d){1,3}"
email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}"
r"(?<!\-)\.)+(?:(?:[a-zA-Z]){2,3}(?:\.(?:[a-zA-Z]){2})?)(?=\b)")
email_validation = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]"
r"{0,62}(?<!\-)\.)+(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)$")
user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
r"(?:(?:[a-zA-Z]){2,3}(?:.(?:[a-zA-Z]){2})?)(?=\b)")
ip = (r"((([0-9]\.)|([0-9][0-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
r"([2][5][0-5]\.))){3}"
r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))$")
number = r"(\-)?(0|[1-9][0-9]*)((\.([0-9])+)?)$"
integer = r"(\-)?(0|[1-9][0-9]*)$"
date = r"(([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1])))"
time = r"((([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9])"
datetime = "(" + date + r"[\sT]" + time + ")$"
domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
'(?:.(?:[a-zA-Z]){2})?)$')
date = "(" + date + ")$"
time = "(" + time + ")$"
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):
modified = self.text
#deal with emails
if self.preserve_email_hostname:
domains = re.findall(email, self.text)
for index, mail in enumerate(domains):
domains[index] = re.search(domain, mail).group(0)
modified = re.sub(email, "[FILTERED]{}", modified). \
format(*domains)
elif self.partially_preserve_email_username:
domains = re.findall(email, self.text)
for index, mail in enumerate(domains):
domains[index] = re.search(domain, mail).group(0)
users = re.findall(email, modified)
for index, item in enumerate(users):
if len(re.match(user, item).group(0)) <= 6:
users[index] = ""
host = [item[:3] + "[FILTERED]" + domains[index]
for index, item in
enumerate(users)]
modified = re.sub(email, "{}", modified). \
format(*host)
else:
modified = re.sub(email, "[EMAIL]", modified)
#deal with phone numbers
modified = re.sub(local, "[PHONE]", modified)
if self.preserve_phone_country_code:
modified = re.sub(inter, "{} [FILTERED]", modified). \
format(*re.findall(country_code, self.text))
else:
modified = re.sub(inter, "[PHONE]", modified)
return modified
class Validations:
@classmethod
def is_email(cls, text):
return bool(re.match(email_validation, text))
@classmethod
def is_phone(cls, text):
return bool(re.match(phone_validation, text))
@classmethod
def is_hostname(cls, text):
return bool(re.match(domain_no_at, text))
@classmethod
def is_ip_address(cls, text):
return bool(re.match(ip, text))
@classmethod
def is_number(cls, text):
return bool(re.match(number, text))
@classmethod
def is_integer(cls, text):
return bool(re.match(integer, text))
@classmethod
def is_date(cls, text):
return bool(re.match(date, text))
@classmethod
def is_time(cls, text):
return bool(re.match(time, text))
@classmethod
def is_datetime(cls, text):
return (bool(re.match(datetime, text)))

Лог от изпълнението

...FF...........F.....FF..FF....F......
======================================================================
FAIL: test_does_not_filter_invalid_emails (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-ww4g2n/test.py", line 48, in test_does_not_filter_invalid_emails
    self.assertEqual(text, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact me here: _invalid@email.com' != 'Contact me here: _[EMAIL]'
- Contact me here: _invalid@email.com
+ Contact me here: _[EMAIL]


======================================================================
FAIL: test_does_not_filter_invalid_phone_numbers (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-ww4g2n/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '+1555 123, 55555' != '[PHONE], 55555'
- +1555 123, 55555
+ [PHONE], 55555


======================================================================
FAIL: test_can_validate_more_complex_phone_numbers (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-ww4g2n/test.py", line 160, in test_can_validate_more_complex_phone_numbers
    self.assertIs(solution.Validations.is_phone(phone), valid)
AssertionError: True is not False

======================================================================
FAIL: test_handles_multiline_strings_in_IP_validation_properly (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-ww4g2n/test.py", line 189, in test_handles_multiline_strings_in_IP_validation_properly
    self.assertFalse(solution.Validations.is_ip_address("8.8.8.8\n"))
AssertionError: True is not false

======================================================================
FAIL: test_handles_multiline_strings_in_hostname_validation_properly (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-ww4g2n/test.py", line 179, in test_handles_multiline_strings_in_hostname_validation_properly
    self.assertFalse(solution.Validations.is_hostname("foo.com\n"))
AssertionError: True is not false

======================================================================
FAIL: test_handles_newlines_in_date_validation (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-ww4g2n/test.py", line 259, in test_handles_newlines_in_date_validation
    self.assertFalse(solution.Validations.is_date("2012-11-19\n"))
AssertionError: True is not false

======================================================================
FAIL: test_handles_newlines_in_time_and_datetime_validation (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-ww4g2n/test.py", line 288, in test_handles_newlines_in_time_and_datetime_validation
    self.assertFalse(solution.Validations.is_time("12:01:01\n"))
AssertionError: True is not false

======================================================================
FAIL: test_validates_hostnames (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-ww4g2n/test.py", line 174, in test_validates_hostnames
    self.assertFalse(solution.Validations.is_hostname('not-a-hostname-.com'))
AssertionError: True is not false

----------------------------------------------------------------------
Ran 39 tests in 0.053s

FAILED (failures=8)

История (9 версии и 0 коментара)

Теодор обнови решението на 21.04.2014 01:23 (преди около 10 години)

+import re
+phone = (r"((?<=\A)(?<=\W)(?<!\+))0(?!0)(([\-\s\(\)]){0,2}\d){6,11}|"
+ r"((((?<=\b)(0{2}))|(\+))(?!0)(\d){1,3}(([\-\s\(\)]){0,2}\d){6,11})")
+local = r"((?<=\A)|(?<=\W)|(?<!\+))0(?!0)(([\-\s\(\)]){0,2}\d){6,11}"
+inter = r"((((?<=\b)(0{2}))|(\+))(?!0)(\d){1,3}(([\-\s\(\)]){0,2}\d){6,11})"
+country_code = r"(?:0{2}|\+)(?!0)\d{1,3}"
+email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
+ "(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)")
+user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
+domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}"
+ "(?:.(?:[a-zA-Z]){2})?)")
+ip = (r"((([0-9]\.)|([0-9][1-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
+ r"([2][5][0-5]\.))){3}"
+ r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))")
+number = r"(\-)?(0*|[1-9][0-9]*)((\.([0-9])+)?)$"
+integer = r"(\-)?(0*|[1-9][0-9]*)$"
+date = r"([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1]))"
+time = r"(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]"
+datetime = date + r"[\sT]" + time
+domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
+ '(?:.(?:[a-zA-Z]){2})?)$')
+
+
+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):
+ #deal with phone numbers
+ modified = self.text
+ modified = re.sub(local, "[PHONE]", modified)
+ if self.preserve_phone_country_code:
+ modified = re.sub(inter, "{} [FILTERED]", modified). \
+ format(*re.findall(country_code, self.text))
+ #deal with emails
+ if self.preserve_email_hostname:
+ modified = re.sub(email, "[FILTERED]{}", modified). \
+ format(*re.findall(domain, self.text))
+ elif self.partially_preserve_email_username:
+ host = [item[:3] + "[FILTERED]" + _ for item in
+ re.findall(user, modified)
+ for _ in re.findall(domain, modified)]
+ modified = re.sub(email, "{}", modified). \
+ format(*host)
+ else:
+ modified = re.sub(email, "[EMAIL]", modified)
+ return modified
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, text):
+ return bool(re.match(email, text))
+
+ @classmethod
+ def is_phone(cls, text):
+ return bool(re.match(phone, text))
+
+ @classmethod
+ def is_hostname(cls, text):
+ return bool(re.match(domain_no_at, text))
+
+ @classmethod
+ def is_ip_address(cls, text):
+ return bool(re.match(ip, text))
+
+ @classmethod
+ def is_number(cls, text):
+ return bool(re.match(number, text))
+
+ @classmethod
+ def is_integer(cls, text):
+ return bool(re.match(integer, text))
+
+ @classmethod
+ def is_date(cls, text):
+ return bool(re.match(date, text))
+
+ @classmethod
+ def is_time(cls, text):
+ return bool(re.match(time, text))
+
+ @classmethod
+ def is_datetime(cls, text):
+ return (bool(re.match(datetime, text)))

Теодор обнови решението на 21.04.2014 02:46 (преди около 10 години)

import re
-phone = (r"((?<=\A)(?<=\W)(?<!\+))0(?!0)(([\-\s\(\)]){0,2}\d){6,11}|"
- r"((((?<=\b)(0{2}))|(\+))(?!0)(\d){1,3}(([\-\s\(\)]){0,2}\d){6,11})")
-local = r"((?<=\A)|(?<=\W)|(?<!\+))0(?!0)(([\-\s\(\)]){0,2}\d){6,11}"
-inter = r"((((?<=\b)(0{2}))|(\+))(?!0)(\d){1,3}(([\-\s\(\)]){0,2}\d){6,11})"
-country_code = r"(?:0{2}|\+)(?!0)\d{1,3}"
+phone = (r"((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11}|"
+ r"((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
+ r"(([\-\s\(\)]){0,2}\d){6,11})")
+local = r"((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11}"
+inter = (r"((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
+ r"(([\-\s\(\)]){0,2}\d){6,11})")
+country_code = r"(?:(?:(?<=\b)(?<!\+)(?:0{2}))|(?:\+))(?!0)(?:\d){1,3}"
email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
"(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)")
user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}"
"(?:.(?:[a-zA-Z]){2})?)")
ip = (r"((([0-9]\.)|([0-9][1-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
r"([2][5][0-5]\.))){3}"
r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))")
number = r"(\-)?(0*|[1-9][0-9]*)((\.([0-9])+)?)$"
integer = r"(\-)?(0*|[1-9][0-9]*)$"
date = r"([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1]))"
time = r"(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]"
datetime = date + r"[\sT]" + time
domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
'(?:.(?:[a-zA-Z]){2})?)$')
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):
#deal with phone numbers
modified = self.text
modified = re.sub(local, "[PHONE]", modified)
if self.preserve_phone_country_code:
modified = re.sub(inter, "{} [FILTERED]", modified). \
format(*re.findall(country_code, self.text))
#deal with emails
if self.preserve_email_hostname:
modified = re.sub(email, "[FILTERED]{}", modified). \
format(*re.findall(domain, self.text))
elif self.partially_preserve_email_username:
host = [item[:3] + "[FILTERED]" + _ for item in
re.findall(user, modified)
for _ in re.findall(domain, modified)]
modified = re.sub(email, "{}", modified). \
format(*host)
else:
modified = re.sub(email, "[EMAIL]", modified)
return modified
class Validations:
@classmethod
def is_email(cls, text):
return bool(re.match(email, text))
@classmethod
def is_phone(cls, text):
return bool(re.match(phone, text))
@classmethod
def is_hostname(cls, text):
return bool(re.match(domain_no_at, text))
@classmethod
def is_ip_address(cls, text):
return bool(re.match(ip, text))
@classmethod
def is_number(cls, text):
return bool(re.match(number, text))
@classmethod
def is_integer(cls, text):
return bool(re.match(integer, text))
@classmethod
def is_date(cls, text):
return bool(re.match(date, text))
@classmethod
def is_time(cls, text):
return bool(re.match(time, text))
@classmethod
def is_datetime(cls, text):
return (bool(re.match(datetime, text)))

Теодор обнови решението на 21.04.2014 17:10 (преди около 10 години)

import re
-phone = (r"((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11}|"
- r"((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
- r"(([\-\s\(\)]){0,2}\d){6,11})")
-local = r"((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11}"
-inter = (r"((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
- r"(([\-\s\(\)]){0,2}\d){6,11})")
+phone = (r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)|"
+ r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
+ r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
+local = r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)"
+inter = (r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
+ r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
country_code = r"(?:(?:(?<=\b)(?<!\+)(?:0{2}))|(?:\+))(?!0)(?:\d){1,3}"
email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
- "(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)")
+ r"(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)(?=\b)")
user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
-domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}"
- "(?:.(?:[a-zA-Z]){2})?)")
-ip = (r"((([0-9]\.)|([0-9][1-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
+domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
+ r"(?:(?:[a-zA-Z]){2,3}(?:.(?:[a-zA-Z]){2})?)(?=\b)")
+ip = (r"((([0-9]\.)|([0-9][0-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
r"([2][5][0-5]\.))){3}"
- r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))")
-number = r"(\-)?(0*|[1-9][0-9]*)((\.([0-9])+)?)$"
-integer = r"(\-)?(0*|[1-9][0-9]*)$"
+ r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))$")
+number = r"(\-)?(0|[1-9][0-9]*)((\.([0-9])+)?)$"
+integer = r"(\-)?(0|[1-9][0-9]*)$"
date = r"([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1]))"
time = r"(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]"
datetime = date + r"[\sT]" + time
domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
'(?:.(?:[a-zA-Z]){2})?)$')
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):
#deal with phone numbers
modified = self.text
modified = re.sub(local, "[PHONE]", modified)
if self.preserve_phone_country_code:
modified = re.sub(inter, "{} [FILTERED]", modified). \
format(*re.findall(country_code, self.text))
+ else:
+ modified = re.sub(inter, "[PHONE]", modified)
#deal with emails
if self.preserve_email_hostname:
modified = re.sub(email, "[FILTERED]{}", modified). \
format(*re.findall(domain, self.text))
elif self.partially_preserve_email_username:
- host = [item[:3] + "[FILTERED]" + _ for item in
- re.findall(user, modified)
- for _ in re.findall(domain, modified)]
+ domains = re.findall(domain, modified)
+ users = re.findall(user, modified)
+ for index, item in enumerate(users):
+ if len(item) <= 7:
+ users[index] = ""
+ host = [item[:3] + "[FILTERED]" + domains[index]
+ for index, item in
+ enumerate(users)]
modified = re.sub(email, "{}", modified). \
format(*host)
else:
modified = re.sub(email, "[EMAIL]", modified)
return modified
class Validations:
@classmethod
def is_email(cls, text):
return bool(re.match(email, text))
@classmethod
def is_phone(cls, text):
return bool(re.match(phone, text))
@classmethod
def is_hostname(cls, text):
return bool(re.match(domain_no_at, text))
@classmethod
def is_ip_address(cls, text):
return bool(re.match(ip, text))
@classmethod
def is_number(cls, text):
return bool(re.match(number, text))
@classmethod
def is_integer(cls, text):
return bool(re.match(integer, text))
@classmethod
def is_date(cls, text):
return bool(re.match(date, text))
@classmethod
def is_time(cls, text):
return bool(re.match(time, text))
@classmethod
def is_datetime(cls, text):
return (bool(re.match(datetime, text)))

Теодор обнови решението на 22.04.2014 23:01 (преди около 10 години)

import re
phone = (r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)|"
r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
+phone_validation = (r"((((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})"
+ r"(?=\b)|(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
+ r"(([\-\s\(\)]){0,2}\d){6,11})))$")
local = r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)"
inter = (r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
country_code = r"(?:(?:(?<=\b)(?<!\+)(?:0{2}))|(?:\+))(?!0)(?:\d){1,3}"
-email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
- r"(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)(?=\b)")
+email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}"
+ r"(?<!\-)\.)+(([a-zA-Z]){2,3}(\.([a-zA-Z]){2})?)(?=\b)")
+email_validation = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]"
+ r"{0,62}(?<!\-)\.)+(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)$")
user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
r"(?:(?:[a-zA-Z]){2,3}(?:.(?:[a-zA-Z]){2})?)(?=\b)")
ip = (r"((([0-9]\.)|([0-9][0-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
r"([2][5][0-5]\.))){3}"
r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))$")
number = r"(\-)?(0|[1-9][0-9]*)((\.([0-9])+)?)$"
integer = r"(\-)?(0|[1-9][0-9]*)$"
date = r"([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1]))"
time = r"(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]"
datetime = date + r"[\sT]" + time
domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
'(?:.(?:[a-zA-Z]){2})?)$')
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):
#deal with phone numbers
modified = self.text
modified = re.sub(local, "[PHONE]", modified)
if self.preserve_phone_country_code:
modified = re.sub(inter, "{} [FILTERED]", modified). \
format(*re.findall(country_code, self.text))
else:
modified = re.sub(inter, "[PHONE]", modified)
#deal with emails
if self.preserve_email_hostname:
modified = re.sub(email, "[FILTERED]{}", modified). \
format(*re.findall(domain, self.text))
elif self.partially_preserve_email_username:
domains = re.findall(domain, modified)
users = re.findall(user, modified)
for index, item in enumerate(users):
if len(item) <= 7:
users[index] = ""
host = [item[:3] + "[FILTERED]" + domains[index]
for index, item in
enumerate(users)]
modified = re.sub(email, "{}", modified). \
format(*host)
else:
modified = re.sub(email, "[EMAIL]", modified)
return modified
class Validations:
@classmethod
def is_email(cls, text):
- return bool(re.match(email, text))
+ return bool(re.match(email_validation, text))
@classmethod
def is_phone(cls, text):
return bool(re.match(phone, text))
@classmethod
def is_hostname(cls, text):
return bool(re.match(domain_no_at, text))
@classmethod
def is_ip_address(cls, text):
return bool(re.match(ip, text))
@classmethod
def is_number(cls, text):
return bool(re.match(number, text))
@classmethod
def is_integer(cls, text):
return bool(re.match(integer, text))
@classmethod
def is_date(cls, text):
return bool(re.match(date, text))
@classmethod
def is_time(cls, text):
return bool(re.match(time, text))
@classmethod
def is_datetime(cls, text):
return (bool(re.match(datetime, text)))

Теодор обнови решението на 22.04.2014 23:02 (преди около 10 години)

import re
phone = (r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)|"
r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
phone_validation = (r"((((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})"
r"(?=\b)|(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11})))$")
local = r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)"
inter = (r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
country_code = r"(?:(?:(?<=\b)(?<!\+)(?:0{2}))|(?:\+))(?!0)(?:\d){1,3}"
email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}"
r"(?<!\-)\.)+(([a-zA-Z]){2,3}(\.([a-zA-Z]){2})?)(?=\b)")
email_validation = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]"
r"{0,62}(?<!\-)\.)+(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)$")
user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
r"(?:(?:[a-zA-Z]){2,3}(?:.(?:[a-zA-Z]){2})?)(?=\b)")
ip = (r"((([0-9]\.)|([0-9][0-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
r"([2][5][0-5]\.))){3}"
r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))$")
number = r"(\-)?(0|[1-9][0-9]*)((\.([0-9])+)?)$"
integer = r"(\-)?(0|[1-9][0-9]*)$"
date = r"([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1]))"
time = r"(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]"
datetime = date + r"[\sT]" + time
domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
'(?:.(?:[a-zA-Z]){2})?)$')
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):
#deal with phone numbers
modified = self.text
modified = re.sub(local, "[PHONE]", modified)
if self.preserve_phone_country_code:
modified = re.sub(inter, "{} [FILTERED]", modified). \
format(*re.findall(country_code, self.text))
else:
modified = re.sub(inter, "[PHONE]", modified)
#deal with emails
if self.preserve_email_hostname:
modified = re.sub(email, "[FILTERED]{}", modified). \
format(*re.findall(domain, self.text))
elif self.partially_preserve_email_username:
domains = re.findall(domain, modified)
users = re.findall(user, modified)
for index, item in enumerate(users):
if len(item) <= 7:
users[index] = ""
host = [item[:3] + "[FILTERED]" + domains[index]
for index, item in
enumerate(users)]
modified = re.sub(email, "{}", modified). \
format(*host)
else:
modified = re.sub(email, "[EMAIL]", modified)
return modified
class Validations:
@classmethod
def is_email(cls, text):
return bool(re.match(email_validation, text))
@classmethod
def is_phone(cls, text):
- return bool(re.match(phone, text))
+ return bool(re.match(phone_validation, text))
@classmethod
def is_hostname(cls, text):
return bool(re.match(domain_no_at, text))
@classmethod
def is_ip_address(cls, text):
return bool(re.match(ip, text))
@classmethod
def is_number(cls, text):
return bool(re.match(number, text))
@classmethod
def is_integer(cls, text):
return bool(re.match(integer, text))
@classmethod
def is_date(cls, text):
return bool(re.match(date, text))
@classmethod
def is_time(cls, text):
return bool(re.match(time, text))
@classmethod
def is_datetime(cls, text):
return (bool(re.match(datetime, text)))

Теодор обнови решението на 22.04.2014 23:15 (преди около 10 години)

import re
phone = (r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)|"
r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
phone_validation = (r"((((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})"
r"(?=\b)|(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11})))$")
local = r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)"
inter = (r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
country_code = r"(?:(?:(?<=\b)(?<!\+)(?:0{2}))|(?:\+))(?!0)(?:\d){1,3}"
email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}"
r"(?<!\-)\.)+(([a-zA-Z]){2,3}(\.([a-zA-Z]){2})?)(?=\b)")
email_validation = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]"
r"{0,62}(?<!\-)\.)+(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)$")
user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
r"(?:(?:[a-zA-Z]){2,3}(?:.(?:[a-zA-Z]){2})?)(?=\b)")
ip = (r"((([0-9]\.)|([0-9][0-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
r"([2][5][0-5]\.))){3}"
r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))$")
number = r"(\-)?(0|[1-9][0-9]*)((\.([0-9])+)?)$"
integer = r"(\-)?(0|[1-9][0-9]*)$"
-date = r"([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1]))"
-time = r"(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]"
-datetime = date + r"[\sT]" + time
+date = r"(([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1])))"
+time = r"((([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9])"
+datetime = "(" + date + r"[\sT]" + time + ")$"
domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
'(?:.(?:[a-zA-Z]){2})?)$')
+date = "(" + date + ")$"
+time = "(" + time + ")$"
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):
#deal with phone numbers
modified = self.text
modified = re.sub(local, "[PHONE]", modified)
if self.preserve_phone_country_code:
modified = re.sub(inter, "{} [FILTERED]", modified). \
format(*re.findall(country_code, self.text))
else:
modified = re.sub(inter, "[PHONE]", modified)
#deal with emails
if self.preserve_email_hostname:
modified = re.sub(email, "[FILTERED]{}", modified). \
format(*re.findall(domain, self.text))
elif self.partially_preserve_email_username:
domains = re.findall(domain, modified)
users = re.findall(user, modified)
for index, item in enumerate(users):
if len(item) <= 7:
users[index] = ""
host = [item[:3] + "[FILTERED]" + domains[index]
for index, item in
enumerate(users)]
modified = re.sub(email, "{}", modified). \
format(*host)
else:
modified = re.sub(email, "[EMAIL]", modified)
return modified
class Validations:
@classmethod
def is_email(cls, text):
return bool(re.match(email_validation, text))
@classmethod
def is_phone(cls, text):
return bool(re.match(phone_validation, text))
@classmethod
def is_hostname(cls, text):
return bool(re.match(domain_no_at, text))
@classmethod
def is_ip_address(cls, text):
return bool(re.match(ip, text))
@classmethod
def is_number(cls, text):
return bool(re.match(number, text))
@classmethod
def is_integer(cls, text):
return bool(re.match(integer, text))
@classmethod
def is_date(cls, text):
return bool(re.match(date, text))
@classmethod
def is_time(cls, text):
return bool(re.match(time, text))
@classmethod
def is_datetime(cls, text):
return (bool(re.match(datetime, text)))

Теодор обнови решението на 23.04.2014 01:28 (преди около 10 години)

import re
phone = (r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)|"
r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
phone_validation = (r"((((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})"
r"(?=\b)|(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11})))$")
local = r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)"
inter = (r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
country_code = r"(?:(?:(?<=\b)(?<!\+)(?:0{2}))|(?:\+))(?!0)(?:\d){1,3}"
-email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]{0,62}"
- r"(?<!\-)\.)+(([a-zA-Z]){2,3}(\.([a-zA-Z]){2})?)(?=\b)")
+email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}"
+ r"(?<!\-)\.)+(?:(?:[a-zA-Z]){2,3}(?:\.(?:[a-zA-Z]){2})?)(?=\b)")
email_validation = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]"
r"{0,62}(?<!\-)\.)+(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)$")
user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
r"(?:(?:[a-zA-Z]){2,3}(?:.(?:[a-zA-Z]){2})?)(?=\b)")
ip = (r"((([0-9]\.)|([0-9][0-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
r"([2][5][0-5]\.))){3}"
r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))$")
number = r"(\-)?(0|[1-9][0-9]*)((\.([0-9])+)?)$"
integer = r"(\-)?(0|[1-9][0-9]*)$"
date = r"(([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1])))"
time = r"((([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9])"
datetime = "(" + date + r"[\sT]" + time + ")$"
domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
'(?:.(?:[a-zA-Z]){2})?)$')
date = "(" + date + ")$"
time = "(" + time + ")$"
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):
- #deal with phone numbers
modified = self.text
- modified = re.sub(local, "[PHONE]", modified)
- if self.preserve_phone_country_code:
- modified = re.sub(inter, "{} [FILTERED]", modified). \
- format(*re.findall(country_code, self.text))
- else:
- modified = re.sub(inter, "[PHONE]", modified)
#deal with emails
if self.preserve_email_hostname:
modified = re.sub(email, "[FILTERED]{}", modified). \
format(*re.findall(domain, self.text))
elif self.partially_preserve_email_username:
domains = re.findall(domain, modified)
- users = re.findall(user, modified)
+ users = re.findall(email, modified)
for index, item in enumerate(users):
- if len(item) <= 7:
+ if len(re.match(user, item).group(0)) <= 7:
users[index] = ""
host = [item[:3] + "[FILTERED]" + domains[index]
for index, item in
enumerate(users)]
modified = re.sub(email, "{}", modified). \
format(*host)
else:
modified = re.sub(email, "[EMAIL]", modified)
+ #deal with phone numbers
+ modified = re.sub(local, "[PHONE]", modified)
+ if self.preserve_phone_country_code:
+ modified = re.sub(inter, "{} [FILTERED]", modified). \
+ format(*re.findall(country_code, self.text))
+ else:
+ modified = re.sub(inter, "[PHONE]", modified)
return modified
class Validations:
@classmethod
def is_email(cls, text):
return bool(re.match(email_validation, text))
@classmethod
def is_phone(cls, text):
return bool(re.match(phone_validation, text))
@classmethod
def is_hostname(cls, text):
return bool(re.match(domain_no_at, text))
@classmethod
def is_ip_address(cls, text):
return bool(re.match(ip, text))
@classmethod
def is_number(cls, text):
return bool(re.match(number, text))
@classmethod
def is_integer(cls, text):
return bool(re.match(integer, text))
@classmethod
def is_date(cls, text):
return bool(re.match(date, text))
@classmethod
def is_time(cls, text):
return bool(re.match(time, text))
@classmethod
def is_datetime(cls, text):
return (bool(re.match(datetime, text)))

Теодор обнови решението на 23.04.2014 01:47 (преди около 10 години)

import re
phone = (r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)|"
r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
phone_validation = (r"((((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})"
r"(?=\b)|(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11})))$")
local = r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)"
inter = (r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
country_code = r"(?:(?:(?<=\b)(?<!\+)(?:0{2}))|(?:\+))(?!0)(?:\d){1,3}"
email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}"
r"(?<!\-)\.)+(?:(?:[a-zA-Z]){2,3}(?:\.(?:[a-zA-Z]){2})?)(?=\b)")
email_validation = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]"
r"{0,62}(?<!\-)\.)+(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)$")
user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
r"(?:(?:[a-zA-Z]){2,3}(?:.(?:[a-zA-Z]){2})?)(?=\b)")
ip = (r"((([0-9]\.)|([0-9][0-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
r"([2][5][0-5]\.))){3}"
r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))$")
number = r"(\-)?(0|[1-9][0-9]*)((\.([0-9])+)?)$"
integer = r"(\-)?(0|[1-9][0-9]*)$"
date = r"(([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1])))"
time = r"((([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9])"
datetime = "(" + date + r"[\sT]" + time + ")$"
domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
'(?:.(?:[a-zA-Z]){2})?)$')
date = "(" + date + ")$"
time = "(" + time + ")$"
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):
modified = self.text
#deal with emails
if self.preserve_email_hostname:
+ domains = re.findall(email, self.text)
+ for index, mail in enumerate(domains):
+ domains[index] = re.search(domain, mail).group(0)
modified = re.sub(email, "[FILTERED]{}", modified). \
- format(*re.findall(domain, self.text))
+ format(*domains)
elif self.partially_preserve_email_username:
- domains = re.findall(domain, modified)
+ domains = re.findall(email, self.text)
+ for index, mail in enumerate(domains):
+ domains[index] = re.search(domain, mail).group(0)
users = re.findall(email, modified)
for index, item in enumerate(users):
if len(re.match(user, item).group(0)) <= 7:
users[index] = ""
host = [item[:3] + "[FILTERED]" + domains[index]
for index, item in
enumerate(users)]
modified = re.sub(email, "{}", modified). \
format(*host)
else:
modified = re.sub(email, "[EMAIL]", modified)
#deal with phone numbers
modified = re.sub(local, "[PHONE]", modified)
if self.preserve_phone_country_code:
modified = re.sub(inter, "{} [FILTERED]", modified). \
format(*re.findall(country_code, self.text))
else:
modified = re.sub(inter, "[PHONE]", modified)
return modified
class Validations:
@classmethod
def is_email(cls, text):
return bool(re.match(email_validation, text))
@classmethod
def is_phone(cls, text):
return bool(re.match(phone_validation, text))
@classmethod
def is_hostname(cls, text):
return bool(re.match(domain_no_at, text))
@classmethod
def is_ip_address(cls, text):
return bool(re.match(ip, text))
@classmethod
def is_number(cls, text):
return bool(re.match(number, text))
@classmethod
def is_integer(cls, text):
return bool(re.match(integer, text))
@classmethod
def is_date(cls, text):
return bool(re.match(date, text))
@classmethod
def is_time(cls, text):
return bool(re.match(time, text))
@classmethod
def is_datetime(cls, text):
return (bool(re.match(datetime, text)))

Теодор обнови решението на 23.04.2014 08:52 (преди около 10 години)

import re
phone = (r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)|"
r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
phone_validation = (r"((((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})"
r"(?=\b)|(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11})))$")
local = r"(((?<![\+a-zA-Z0-9]))0(?!0)(([\-\s\(\)]){0,2}\d){6,11})(?=\b)"
inter = (r"(((((?<=\b)(?<!\+)(0{2}))|(\+))(?!0)(\d){1,3}"
r"(([\-\s\(\)]){0,2}\d){6,11}))(?=\b)")
country_code = r"(?:(?:(?<=\b)(?<!\+)(?:0{2}))|(?:\+))(?!0)(?:\d){1,3}"
email = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}"
r"(?<!\-)\.)+(?:(?:[a-zA-Z]){2,3}(?:\.(?:[a-zA-Z]){2})?)(?=\b)")
email_validation = (r"[a-zA-Z0-9][\w\+\-\.]{0,200}@([a-zA-Z0-9][a-zA-Z0-9\-]"
r"{0,62}(?<!\-)\.)+(([a-zA-Z]){2,3}(.([a-zA-Z]){2})?)$")
user = r"[a-zA-Z0-9][\w\+\-\.]{0,200}@"
domain = (r"@(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,62}\.)+"
r"(?:(?:[a-zA-Z]){2,3}(?:.(?:[a-zA-Z]){2})?)(?=\b)")
ip = (r"((([0-9]\.)|([0-9][0-9]\.)|([1][0-9][0-9]\.)|([2][0-4][0-9]\.)|"
r"([2][5][0-5]\.))){3}"
r"(([1][0-9][0-9])|([2][0-4][0-9])|([2][5][0-5])|([0-9][1-9])|([0-9]))$")
number = r"(\-)?(0|[1-9][0-9]*)((\.([0-9])+)?)$"
integer = r"(\-)?(0|[1-9][0-9]*)$"
date = r"(([0-9]{4})-((1[0-2])|(0[1-9]))-((0[1-9])|([1-2][0-9])|(3[0-1])))"
time = r"((([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9])"
datetime = "(" + date + r"[\sT]" + time + ")$"
domain_no_at = (r'(?:[a-zA-Z0-9][\w\-]{0,62}\.)+(?:(?:[a-zA-Z]){2,3}'
'(?:.(?:[a-zA-Z]){2})?)$')
date = "(" + date + ")$"
time = "(" + time + ")$"
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):
modified = self.text
#deal with emails
if self.preserve_email_hostname:
domains = re.findall(email, self.text)
for index, mail in enumerate(domains):
domains[index] = re.search(domain, mail).group(0)
modified = re.sub(email, "[FILTERED]{}", modified). \
format(*domains)
elif self.partially_preserve_email_username:
domains = re.findall(email, self.text)
for index, mail in enumerate(domains):
domains[index] = re.search(domain, mail).group(0)
users = re.findall(email, modified)
for index, item in enumerate(users):
- if len(re.match(user, item).group(0)) <= 7:
+ if len(re.match(user, item).group(0)) <= 6:
users[index] = ""
host = [item[:3] + "[FILTERED]" + domains[index]
for index, item in
enumerate(users)]
modified = re.sub(email, "{}", modified). \
format(*host)
else:
modified = re.sub(email, "[EMAIL]", modified)
#deal with phone numbers
modified = re.sub(local, "[PHONE]", modified)
if self.preserve_phone_country_code:
modified = re.sub(inter, "{} [FILTERED]", modified). \
format(*re.findall(country_code, self.text))
else:
modified = re.sub(inter, "[PHONE]", modified)
return modified
class Validations:
@classmethod
def is_email(cls, text):
return bool(re.match(email_validation, text))
@classmethod
def is_phone(cls, text):
return bool(re.match(phone_validation, text))
@classmethod
def is_hostname(cls, text):
return bool(re.match(domain_no_at, text))
@classmethod
def is_ip_address(cls, text):
return bool(re.match(ip, text))
@classmethod
def is_number(cls, text):
return bool(re.match(number, text))
@classmethod
def is_integer(cls, text):
return bool(re.match(integer, text))
@classmethod
def is_date(cls, text):
return bool(re.match(date, text))
@classmethod
def is_time(cls, text):
return bool(re.match(time, text))
@classmethod
def is_datetime(cls, text):
return (bool(re.match(datetime, text)))