Решение на Регулярни изрази от Ралица Цанова

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

Към профила на Ралица Цанова

Резултати

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

Код

import re
PHONE_PREFIX = r'(((\+|00)[1-9]\d{,2})|0)'
PHONE = PHONE_PREFIX + r'([-\s\(\)]{1,2}\d|[1-9])([-\s\(\)]{,2}\d){5,10}'
INTER = r'((\+|\b00)[1-9]\d{,2})[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
HOST = r'(([^\W_](([^\W_]|-){,61}[^\W_])?\.)+[a-zA-Z]{2,3}(\.[a-zA-z]{2})?)'
EMAIL = r'([^\W_][\w\.\+-]{,2})[\w\.\+-]{,198}@' + HOST
DATE = r'\d{4}-(0[1-9]|1[0-2])-(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])'
IP_SECOND_PART = r'(\.(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])){3}'
IP = r'(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])' + IP_SECOND_PART
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):
return self.phone_filtered(self.email_filtered())
def phone_filtered(self, text):
if self.preserve_phone_country_code:
return self.phone_filtration(self.preserve_country_code(text))
return self.phone_filtration(text)
def phone_filtration(self, text):
return re.sub(r'(((\+|\b00)[1-9]\d{,2})|(?<!\+)\b0)' +
r'([-\s\(\)]{1,2}\d|[1-9])' +
r'([-\s\(\)]{,2}\d){5,10}\b',
'[PHONE]', text)
def email_filtered(self):
if self.partially_preserve_email_username:
return self.preserve_hostname(self.partially_preserve_username())
elif self.preserve_email_hostname:
return self.preserve_hostname(self.text)
return re.sub(r'\b' + EMAIL + r'\b', '[EMAIL]', self.text)
def preserve_country_code(self, text):
return re.sub(INTER + r'\b', r'\1 [FILTERED]', text)
def preserve_hostname(self, text):
return re.sub(r'\b' + EMAIL + r'\b', r'[FILTERED]@\2', text)
def partially_preserve_username(self):
return re.sub(r'\b' + r'([^\W_][\w\.\+-]{2})[\w\.\+-]{3,198}@' +
HOST + r'\b', r'\1[FILTERED]@\2', self.text)
class Validations:
@classmethod
def is_email(cls, value):
return re.match(r'^\b' + EMAIL + r'\b$', value) is not None
@classmethod
def is_phone(cls, value):
return re.match(r'^' + PHONE + r'$', value) is not None
@classmethod
def is_hostname(cls, value):
return re.match(r'^\b' + HOST + r'\b$', value) is not None
@classmethod
def is_ip_address(cls, value):
return re.match(r'^\b' + IP + r'\b$', value) is not None
@classmethod
def is_number(cls, value):
return re.match(r'^-?(0|\d+\.\d+|[1-9]\d*)$', value) is not None
@classmethod
def is_integer(cls, value):
return re.match(r'^-?(0|[1-9]\d*)$', value) is not None
@classmethod
def is_date(cls, value):
return re.match(r'^\b' + DATE + r'\b$', value) is not None
@classmethod
def is_time(cls, value):
return re.match(r'^\b' + TIME + r'\b$', value) is not None
@classmethod
def is_datetime(cls, value):
return re.match(r'^\b' + DATE + r'(\s|T)' + TIME + r'\b$',
value) is not None

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

....F...........F.....FF..FF...........
======================================================================
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-1aufyog/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-1aufyog/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-1aufyog/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-1aufyog/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-1aufyog/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-1aufyog/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

----------------------------------------------------------------------
Ran 39 tests in 0.049s

FAILED (failures=6)

История (5 версии и 1 коментар)

Ралица обнови решението на 19.04.2014 16:35 (преди над 10 години)

+import re
+
+
+PHONE = r'(((\+|00)[1-9]\d{,2})|0)[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
+INTER = r'((\+|\b00)[1-9]\d{,2})[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
+HOST = r'(([^\W_](([^\W_]|-){,61}[^\W_])?\.)+[a-zA-Z]{2,3}(\.[a-zA-z]{2})?)'
+EMAIL = r'([^\W_][\w\.\+-]{,2})[\w\.\+-]{,198}@' + HOST
+DATE = r'\d{4}\-(0[1-9]|1[0-2])\-(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])'
+IP = r'(0|[1-9]\d?|1\d\d|2[0-5][0-5])(\.(0|[1-9]\d?|1\d\d|2[0-5][0-5])){3}'
+
+
+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):
+ return self.phone_filtered(self.email_filtered())
+
+ def phone_filtered(self, text):
+ if self.preserve_phone_country_code:
+ return self.phone_filtration(self.preserve_country_code(text))
+ return self.phone_filtration(text)
+
+ def phone_filtration(self, text):
+ return re.sub(r'(((\+|\b00)[1-9]\d{,2})|\b0)[-\s\(\)]{,2}[1-9]'
+ + r'([-\s\(\)]{,2}\d){5,10}\b',
+ '[PHONE]', text)
+
+ def email_filtered(self):
+ if self.partially_preserve_email_username:
+ return self.preserve_hostname(self.partially_preserve_username())
+ elif self.preserve_email_hostname:
+ return self.preserve_hostname(self.text)
+ return re.sub(r'\b' + EMAIL + r'\b', '[EMAIL]', self.text)
+
+ def preserve_country_code(self, text):
+ return re.sub(INTER + r'\b', r'\1 [FILTERED]', text)
+
+ def preserve_hostname(self, text):
+ return re.sub(r'\b' + EMAIL + r'\b', r'[FILTERED]@\2', text)
+
+ def partially_preserve_username(self):
+ return re.sub(r'\b' + r'([^\W_][\w\.\+-]{2})[\w\.\+-]{3,198}@' +
+ HOST + r'\b', r'\1[FILTERED]@\2', self.text)
+
+
+class Validations:
+ @classmethod
+ def is_email(cls, value):
+ return re.match(r'^' + EMAIL + r'$', value) is not None
+
+ @classmethod
+ def is_phone(cls, value):
+ return re.match(r'^' + PHONE + r'$', value) is not None
+
+ @classmethod
+ def is_hostname(cls, value):
+ return re.match(r'^' + HOST + r'$', value) is not None
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return re.match(r'^' + IP + r'$', value) is not None
+
+ @classmethod
+ def is_number(cls, value):
+ return re.match(r'^\-?(0|\d\.\d+|[1-9]\d*)$', value) is not None
+
+ @classmethod
+ def is_integer(cls, value):
+ return re.match(r'^\-?(0|[1-9]\d*)$', value) is not None
+
+ @classmethod
+ def is_date(cls, value):
+ return re.match(r'^' + DATE + r'$', value) is not None
+
+ @classmethod
+ def is_time(cls, value):
+ return re.match(r'^' + TIME + r'$', value) is not None
+
+ @classmethod
+ def is_datetime(cls, value):
+ return re.match(r'^' + DATE + r'\s' + TIME + r'$', value) is not None

Ралица обнови решението на 20.04.2014 02:04 (преди над 10 години)

import re
PHONE = r'(((\+|00)[1-9]\d{,2})|0)[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
INTER = r'((\+|\b00)[1-9]\d{,2})[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
HOST = r'(([^\W_](([^\W_]|-){,61}[^\W_])?\.)+[a-zA-Z]{2,3}(\.[a-zA-z]{2})?)'
EMAIL = r'([^\W_][\w\.\+-]{,2})[\w\.\+-]{,198}@' + HOST
DATE = r'\d{4}\-(0[1-9]|1[0-2])\-(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])'
-IP = r'(0|[1-9]\d?|1\d\d|2[0-5][0-5])(\.(0|[1-9]\d?|1\d\d|2[0-5][0-5])){3}'
+IP_SECOND_PART = r'(\.(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])){3}'
+IP = r'(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])' + IP_SECOND_PART
+
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):
return self.phone_filtered(self.email_filtered())
def phone_filtered(self, text):
if self.preserve_phone_country_code:
return self.phone_filtration(self.preserve_country_code(text))
return self.phone_filtration(text)
def phone_filtration(self, text):
- return re.sub(r'(((\+|\b00)[1-9]\d{,2})|\b0)[-\s\(\)]{,2}[1-9]'
+ return re.sub(r'(((\+|\b00)[1-9]\d{,2})|(?<!\+)\b0)[-\s\(\)]{,2}[1-9]'
+ r'([-\s\(\)]{,2}\d){5,10}\b',
'[PHONE]', text)
def email_filtered(self):
if self.partially_preserve_email_username:
return self.preserve_hostname(self.partially_preserve_username())
elif self.preserve_email_hostname:
return self.preserve_hostname(self.text)
return re.sub(r'\b' + EMAIL + r'\b', '[EMAIL]', self.text)
def preserve_country_code(self, text):
return re.sub(INTER + r'\b', r'\1 [FILTERED]', text)
def preserve_hostname(self, text):
return re.sub(r'\b' + EMAIL + r'\b', r'[FILTERED]@\2', text)
def partially_preserve_username(self):
return re.sub(r'\b' + r'([^\W_][\w\.\+-]{2})[\w\.\+-]{3,198}@' +
HOST + r'\b', r'\1[FILTERED]@\2', self.text)
class Validations:
@classmethod
def is_email(cls, value):
return re.match(r'^' + EMAIL + r'$', value) is not None
@classmethod
def is_phone(cls, value):
return re.match(r'^' + PHONE + r'$', value) is not None
@classmethod
def is_hostname(cls, value):
return re.match(r'^' + HOST + r'$', value) is not None
@classmethod
def is_ip_address(cls, value):
return re.match(r'^' + IP + r'$', value) is not None
@classmethod
def is_number(cls, value):
return re.match(r'^\-?(0|\d\.\d+|[1-9]\d*)$', value) is not None
@classmethod
def is_integer(cls, value):
return re.match(r'^\-?(0|[1-9]\d*)$', value) is not None
@classmethod
def is_date(cls, value):
return re.match(r'^' + DATE + r'$', value) is not None
@classmethod
def is_time(cls, value):
return re.match(r'^' + TIME + r'$', value) is not None
@classmethod
def is_datetime(cls, value):
- return re.match(r'^' + DATE + r'\s' + TIME + r'$', value) is not None
+ return re.match(r'^' + DATE + r'(\s|T)' + TIME + r'$', value) is not None

Ралица обнови решението на 22.04.2014 23:09 (преди над 10 години)

import re
PHONE = r'(((\+|00)[1-9]\d{,2})|0)[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
INTER = r'((\+|\b00)[1-9]\d{,2})[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
HOST = r'(([^\W_](([^\W_]|-){,61}[^\W_])?\.)+[a-zA-Z]{2,3}(\.[a-zA-z]{2})?)'
EMAIL = r'([^\W_][\w\.\+-]{,2})[\w\.\+-]{,198}@' + HOST
-DATE = r'\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|[1-2][0-9]|3[0-1])'
+DATE = r'\d{4}-(0[1-9]|1[0-2])-(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])'
IP_SECOND_PART = r'(\.(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])){3}'
IP = r'(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])' + IP_SECOND_PART
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):
return self.phone_filtered(self.email_filtered())
def phone_filtered(self, text):
if self.preserve_phone_country_code:
return self.phone_filtration(self.preserve_country_code(text))
return self.phone_filtration(text)
def phone_filtration(self, text):
return re.sub(r'(((\+|\b00)[1-9]\d{,2})|(?<!\+)\b0)[-\s\(\)]{,2}[1-9]'
+ r'([-\s\(\)]{,2}\d){5,10}\b',
'[PHONE]', text)
def email_filtered(self):
if self.partially_preserve_email_username:
return self.preserve_hostname(self.partially_preserve_username())
elif self.preserve_email_hostname:
return self.preserve_hostname(self.text)
return re.sub(r'\b' + EMAIL + r'\b', '[EMAIL]', self.text)
def preserve_country_code(self, text):
return re.sub(INTER + r'\b', r'\1 [FILTERED]', text)
def preserve_hostname(self, text):
return re.sub(r'\b' + EMAIL + r'\b', r'[FILTERED]@\2', text)
def partially_preserve_username(self):
return re.sub(r'\b' + r'([^\W_][\w\.\+-]{2})[\w\.\+-]{3,198}@' +
HOST + r'\b', r'\1[FILTERED]@\2', self.text)
class Validations:
@classmethod
def is_email(cls, value):
return re.match(r'^' + EMAIL + r'$', value) is not None
@classmethod
def is_phone(cls, value):
return re.match(r'^' + PHONE + r'$', value) is not None
@classmethod
def is_hostname(cls, value):
return re.match(r'^' + HOST + r'$', value) is not None
@classmethod
def is_ip_address(cls, value):
return re.match(r'^' + IP + r'$', value) is not None
@classmethod
def is_number(cls, value):
- return re.match(r'^\-?(0|\d\.\d+|[1-9]\d*)$', value) is not None
+ return re.match(r'^-?(0|\d+\.\d+|[1-9]\d*)$', value) is not None
@classmethod
def is_integer(cls, value):
- return re.match(r'^\-?(0|[1-9]\d*)$', value) is not None
+ return re.match(r'^-?(0|[1-9]\d*)$', value) is not None
@classmethod
def is_date(cls, value):
return re.match(r'^' + DATE + r'$', value) is not None
@classmethod
def is_time(cls, value):
return re.match(r'^' + TIME + r'$', value) is not None
@classmethod
def is_datetime(cls, value):
return re.match(r'^' + DATE + r'(\s|T)' + TIME + r'$', value) is not None

Ралица обнови решението на 22.04.2014 23:54 (преди над 10 години)

import re
-PHONE = r'(((\+|00)[1-9]\d{,2})|0)[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
+PHONE_PREFIX = r'(((\+|00)[1-9]\d{,2})|0)'
+PHONE = PHONE_PREFIX + r'([-\s\(\)]{1,2}\d|[1-9])([-\s\(\)]{,2}\d){5,10}'
INTER = r'((\+|\b00)[1-9]\d{,2})[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
HOST = r'(([^\W_](([^\W_]|-){,61}[^\W_])?\.)+[a-zA-Z]{2,3}(\.[a-zA-z]{2})?)'
EMAIL = r'([^\W_][\w\.\+-]{,2})[\w\.\+-]{,198}@' + HOST
DATE = r'\d{4}-(0[1-9]|1[0-2])-(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])'
IP_SECOND_PART = r'(\.(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])){3}'
IP = r'(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])' + IP_SECOND_PART
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):
return self.phone_filtered(self.email_filtered())
def phone_filtered(self, text):
if self.preserve_phone_country_code:
return self.phone_filtration(self.preserve_country_code(text))
return self.phone_filtration(text)
def phone_filtration(self, text):
- return re.sub(r'(((\+|\b00)[1-9]\d{,2})|(?<!\+)\b0)[-\s\(\)]{,2}[1-9]'
- + r'([-\s\(\)]{,2}\d){5,10}\b',
+ return re.sub(r'(((\+|\b00)[1-9]\d{,2})|(?<!\+)\b0)' +
+ r'([-\s\(\)]{1,2}\d|[1-9])' +
+ r'([-\s\(\)]{,2}\d){5,10}\b',
'[PHONE]', text)
def email_filtered(self):
if self.partially_preserve_email_username:
return self.preserve_hostname(self.partially_preserve_username())
elif self.preserve_email_hostname:
return self.preserve_hostname(self.text)
return re.sub(r'\b' + EMAIL + r'\b', '[EMAIL]', self.text)
def preserve_country_code(self, text):
return re.sub(INTER + r'\b', r'\1 [FILTERED]', text)
def preserve_hostname(self, text):
return re.sub(r'\b' + EMAIL + r'\b', r'[FILTERED]@\2', text)
def partially_preserve_username(self):
return re.sub(r'\b' + r'([^\W_][\w\.\+-]{2})[\w\.\+-]{3,198}@' +
HOST + r'\b', r'\1[FILTERED]@\2', self.text)
class Validations:
@classmethod
def is_email(cls, value):
return re.match(r'^' + EMAIL + r'$', value) is not None
@classmethod
def is_phone(cls, value):
return re.match(r'^' + PHONE + r'$', value) is not None
@classmethod
def is_hostname(cls, value):
return re.match(r'^' + HOST + r'$', value) is not None
@classmethod
def is_ip_address(cls, value):
return re.match(r'^' + IP + r'$', value) is not None
@classmethod
def is_number(cls, value):
return re.match(r'^-?(0|\d+\.\d+|[1-9]\d*)$', value) is not None
@classmethod
def is_integer(cls, value):
return re.match(r'^-?(0|[1-9]\d*)$', value) is not None
@classmethod
def is_date(cls, value):
return re.match(r'^' + DATE + r'$', value) is not None
@classmethod
def is_time(cls, value):
return re.match(r'^' + TIME + r'$', value) is not None
@classmethod
def is_datetime(cls, value):
return re.match(r'^' + DATE + r'(\s|T)' + TIME + r'$', value) is not None

Ралица обнови решението на 23.04.2014 01:26 (преди над 10 години)

import re
PHONE_PREFIX = r'(((\+|00)[1-9]\d{,2})|0)'
PHONE = PHONE_PREFIX + r'([-\s\(\)]{1,2}\d|[1-9])([-\s\(\)]{,2}\d){5,10}'
INTER = r'((\+|\b00)[1-9]\d{,2})[-\s\(\)]{,2}[1-9]([-\s\(\)]{,2}\d){5,10}'
HOST = r'(([^\W_](([^\W_]|-){,61}[^\W_])?\.)+[a-zA-Z]{2,3}(\.[a-zA-z]{2})?)'
EMAIL = r'([^\W_][\w\.\+-]{,2})[\w\.\+-]{,198}@' + HOST
DATE = r'\d{4}-(0[1-9]|1[0-2])-(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])'
IP_SECOND_PART = r'(\.(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])){3}'
IP = r'(0|[1-9]\d?|1\d\d|2[0-4]\d|25[0-5])' + IP_SECOND_PART
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):
return self.phone_filtered(self.email_filtered())
def phone_filtered(self, text):
if self.preserve_phone_country_code:
return self.phone_filtration(self.preserve_country_code(text))
return self.phone_filtration(text)
def phone_filtration(self, text):
return re.sub(r'(((\+|\b00)[1-9]\d{,2})|(?<!\+)\b0)' +
r'([-\s\(\)]{1,2}\d|[1-9])' +
r'([-\s\(\)]{,2}\d){5,10}\b',
'[PHONE]', text)
def email_filtered(self):
if self.partially_preserve_email_username:
return self.preserve_hostname(self.partially_preserve_username())
elif self.preserve_email_hostname:
return self.preserve_hostname(self.text)
return re.sub(r'\b' + EMAIL + r'\b', '[EMAIL]', self.text)
def preserve_country_code(self, text):
return re.sub(INTER + r'\b', r'\1 [FILTERED]', text)
def preserve_hostname(self, text):
return re.sub(r'\b' + EMAIL + r'\b', r'[FILTERED]@\2', text)
def partially_preserve_username(self):
return re.sub(r'\b' + r'([^\W_][\w\.\+-]{2})[\w\.\+-]{3,198}@' +
HOST + r'\b', r'\1[FILTERED]@\2', self.text)
class Validations:
@classmethod
def is_email(cls, value):
- return re.match(r'^' + EMAIL + r'$', value) is not None
+ return re.match(r'^\b' + EMAIL + r'\b$', value) is not None
@classmethod
def is_phone(cls, value):
return re.match(r'^' + PHONE + r'$', value) is not None
@classmethod
def is_hostname(cls, value):
- return re.match(r'^' + HOST + r'$', value) is not None
+ return re.match(r'^\b' + HOST + r'\b$', value) is not None
@classmethod
def is_ip_address(cls, value):
- return re.match(r'^' + IP + r'$', value) is not None
+ return re.match(r'^\b' + IP + r'\b$', value) is not None
@classmethod
def is_number(cls, value):
return re.match(r'^-?(0|\d+\.\d+|[1-9]\d*)$', value) is not None
@classmethod
def is_integer(cls, value):
return re.match(r'^-?(0|[1-9]\d*)$', value) is not None
@classmethod
def is_date(cls, value):
- return re.match(r'^' + DATE + r'$', value) is not None
+ return re.match(r'^\b' + DATE + r'\b$', value) is not None
@classmethod
def is_time(cls, value):
- return re.match(r'^' + TIME + r'$', value) is not None
+ return re.match(r'^\b' + TIME + r'\b$', value) is not None
@classmethod
def is_datetime(cls, value):
- return re.match(r'^' + DATE + r'(\s|T)' + TIME + r'$', value) is not None
+ return re.match(r'^\b' + DATE + r'(\s|T)' + TIME + r'\b$',
+ value) is not None