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

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

Към профила на Божидар Карааргиров

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 28 успешни тест(а)
  • 11 неуспешни тест(а)

Код

import re
class Validations:
@classmethod
def is_hostname(cls, value):
domain = '([a-zA-Z0-9]{1}([a-zA-Z0-9]|-){0,61}[a-zA-Z0-9]{0,1})'
tld = '(\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,1})$'
pattern = re.compile('^(' + domain + '\.)*' + domain + tld)
return bool(re.search(pattern, value))
@classmethod
def is_email(cls, value):
if bool(re.search('@{1}', value)) is False:
return False
email_pattern = r'^([a-zA-Z0-9]){1}([a-zA-Z0-9]|_|\+|\.|-){0,200}$'
s = value.split('@')
return bool(re.search(email_pattern, s[0])) and cls.is_hostname(s[1])
@classmethod
def is_ip_address(cls, value):
number_range = '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
pattern = re.compile('^(' + number_range + '\.){3}' + number_range + '$')
return bool(re.search(pattern, value))
@classmethod
def is_number(cls, value):
pattern = r'^(-)?(0|[1-9][0-9]*)(\.[0-9]+)?$'
return bool(re.search(pattern, value))
@classmethod
def is_integer(cls, value):
pattern = r'^(-)?(0|[1-9][0-9]*)$'
return bool(re.search(pattern, value))
@classmethod
def is_date(cls, value):
pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$'
return bool(re.search(pattern, value))
@classmethod
def is_time(cls, value):
pattern = r'^([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
return bool(re.search(pattern, value))
@classmethod
def is_datetime(cls, value):
pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])(\s|T|-)([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
return bool(re.search(pattern, value))
@classmethod
def is_phone(cls, value):
interval_pattern = r"((\,|\s|\(|\)){0,2})"
base_digit_pattern = r'(' + interval_pattern + '?[0-9])'
local_pattern = re.compile(r'^0' + interval_pattern + '?[1-9]' + base_digit_pattern + '{5,10}$')
international_pattern = re.compile(r'^(00|\+)' + '?[1-9][0-9]{0,2}' + base_digit_pattern + '{6,11}$')
return bool(re.search(local_pattern, value)) or bool(re.search(international_pattern, value))
class PrivacyFilter:
domain = r'(?:[a-zA-Z0-9]{1}(?:[a-zA-Z0-9]|-){0,61}[a-zA-Z0-9]{0,1})'
tld = r'(?:\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,1})'
email_pattern = re.compile(r'(\b(?:[a-zA-Z0-9]){1}(?:[a-zA-Z0-9]|\s|\+|\.|-){0,200}' + '@' + '(?:' + domain + '\.)*' + domain + tld + r'\b)')
interval_pattern = r'(?:(?:\,|\s|\(|\)){0,2})'
base_digit_pattern = r'(?:' + interval_pattern + '?[0-9])'
local_pattern = re.compile(r'0' + interval_pattern + '?[1-9]' + base_digit_pattern + '{5,10}')
international_pattern = re.compile(r'((?:00|\+)' + '?[1-9][0-9]{0,2}' + base_digit_pattern + '{6,11})')
def __init__(self, text):
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
self.text = text
def filtered(self):
self.filter()
return self.text
def filter(self):
emails_to_be_filtered = re.findall(self.email_pattern, self.text)
if self.preserve_email_hostname == False and self.partially_preserve_email_username == False:
self.text = re.sub(self.email_pattern, "[EMAIL]", self.text)
elif self.partially_preserve_email_username == True:
for email in emails_to_be_filtered:
email_parts = email[0].split('@')
if len(email_parts[0]) >= 6:
email_parts[0] = email_parts[0][0:3] + "[FILTERED]@"
self.text = self.text.replace(email[0], email_parts[0] + email_parts[1])
else:
self.text = self.text.replace(email[0], "[FILTERED]@" + email_parts[1])
else:
for email in emails_to_be_filtered:
self.text = self.text.replace(email[0], re.sub('([a-zA-Z0-9]|_|\.|-){1,201}@',"[FILTERED]@",email[0]))
self.text = re.sub(self.local_pattern, "[PHONE]", self.text)
phones_to_be_filtered = re.findall(self.international_pattern, self.text)
if phones_to_be_filtered != ['']:
if self.preserve_phone_country_code == True:
for phone in phones_to_be_filtered:
phone_digits = ''.join(digit for digit in phone[0] if digit.isdigit())
code = ""
if phone_digits[0] is '0':
phone_digits = phone_digits[2:]
code = "00"
else:
code = "+"
if len(phone_digits) >= 9:
code = code + phone_digits[0:3]
elif len(phone_digits) == 8:
code = code + phone_digits[0:2]
else:
code = code + phone_digits[0]
self.text = self.text.replace(phone[0], code + " [FILTERED]")
else:
for phone in phones_to_be_filtered:
self.text = self.text.replace(phone[0],"[PHONE]")

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

F...FF...FF.....F.....FF..FF....F......
======================================================================
FAIL: test_allows_email_hostname_to_be_preserved (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-vaycj0/test.py", line 55, in test_allows_email_hostname_to_be_preserved
    self.assertEqual('[FILTERED]@exa.mple.com', self.filter_email_usernames('some12-+3@exa.mple.com'))
AssertionError: '[FILTERED]@exa.mple.com' != 'some12-+[FILTERED]@exa.mple.com'
- [FILTERED]@exa.mple.com
+ some12-+[FILTERED]@exa.mple.com
? ++++++++


======================================================================
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-vaycj0/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '+1555 123, 55555' != '[PHONE]1555 123, 55555'
- +1555 123, 55555
? ^
+ [PHONE]1555 123, 55555
? ^^^^^^^


======================================================================
FAIL: test_filters_more_complex_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-vaycj0/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[PHONE]' != '[PHONE]155512345699'
- [PHONE]
+ [PHONE]155512345699


======================================================================
FAIL: test_preserves_whitespace_around_phones (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-vaycj0/test.py", line 89, in test_preserves_whitespace_around_phones
    self.assertEqual(' [PHONE] or...', solution.PrivacyFilter(' +359881212-12-1 2 or...').filtered())
AssertionError: ' [PHONE] or...' != ' [PHONE]359881212-12-1 2 or...'
-  [PHONE] or...
+  [PHONE]359881212-12-1 2 or...


======================================================================
FAIL: test_separates_preserved_country_code_from_filtered_phone_with_a_space (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-vaycj0/test.py", line 100, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    self.assertEqual(filtered, filter.filtered())
AssertionError: 'Phone: 0025 [FILTERED]' != 'Phone: 0[PHONE]'
- Phone: 0025 [FILTERED]
+ Phone: 0[PHONE]


======================================================================
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-vaycj0/test.py", line 160, in test_can_validate_more_complex_phone_numbers
    self.assertIs(solution.Validations.is_phone(phone), valid)
AssertionError: False is not True

======================================================================
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-vaycj0/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-vaycj0/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-vaycj0/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-vaycj0/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-vaycj0/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.064s

FAILED (failures=11)

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

Божидар обнови решението на 23.04.2014 14:18 (преди над 10 години)

+import re
+
+
+class Validations:
+ @classmethod
+ def is_hostname(cls, value):
+ domain = '([a-zA-Z0-9]{1}([a-zA-Z0-9]|-){0,61}[a-zA-Z0-9]{0,1})'
+ tld = '(\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,1})$'
+ pattern = re.compile('^(' + domain + '\.)*' + domain + tld)
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_email(cls, value):
+ if bool(re.search('@{1}', value)) is False:
+ return False
+ email_pattern = r'^([a-zA-Z0-9]){1}([a-zA-Z0-9]|_|\+|\.|-){0,200}$'
+ s = value.split('@')
+ return bool(re.search(email_pattern, s[0])) and cls.is_hostname(s[1])
+
+ @classmethod
+ def is_ip_address(cls, value):
+ number_range = '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
+ pattern = re.compile('^(' + number_range + '\.){3}' + number_range + '$')
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_number(cls, value):
+ pattern = r'^(-)?(0|[1-9][0-9]*)(\.[0-9]+)?$'
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_integer(cls, value):
+ pattern = r'^(-)?(0|[1-9][0-9]*)$'
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_date(cls, value):
+ pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$'
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_time(cls, value):
+ pattern = r'^([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_datetime (cls, value):
+ pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])(\s|T|-)([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
+ return bool(re.search(pattern, value))
+
+ @classmethod
+ def is_phone(cls, value):
+ interval_pattern = r"((\,|\s|\(|\)){0,2})"
+ base_digit_pattern = r'(' + interval_pattern + '?[0-9])'
+ local_pattern = re.compile(r'^0' + interval_pattern + '?[1-9]' + base_digit_pattern + '{5,10}$')
+ international_pattern = re.compile(r'^(00|'+')'+ \
+ '?[1-9]' + base_digit_pattern +'{0,2}' + base_digit_pattern + '{6,11}$')
+ return bool(re.search(local_pattern, value)) or bool(re.search(international_pattern, value))
+
+
+class PrivacyFilter:
+ domain = r'(?:[a-zA-Z0-9]{1}(?:[a-zA-Z0-9]|-){0,61}[a-zA-Z0-9]{0,1})'
+ tld = r'(?:\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,1})'
+ email_pattern = re.compile(r'(\b(?:[a-zA-Z0-9]){1}(?:[a-zA-Z0-9]|\s|\+|\.|-){0,200}' + '@' + '(?:' + domain + '\.)*' + domain + tld + r'\b)')
+ interval_pattern = r'(?:(?:\,|\s|\(|\)){0,2})'
+ base_digit_pattern = r'(?:' + interval_pattern + '?[0-9])'
+ local_pattern = re.compile(r'0' + interval_pattern + '?[1-9]' + base_digit_pattern + '{5,10}')
+ international_pattern = re.compile(r'((?:00|\+)'+ \
+ '?[1-9]' + base_digit_pattern +'{0,2}' + base_digit_pattern + '{6,11})')
+
+ def __init__(self, text):
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+ self.text = text
+
+ def filtered(self):
+ self.filter()
+ return self.text
+
+ def filter(self):
+ emails_to_be_filtered = re.findall(self.email_pattern, self.text)
+ if self.preserve_email_hostname == False and self.partially_preserve_email_username == False:
+ self.text = re.sub(self.email_pattern, "[EMAIL]", self.text)
+ elif self.partially_preserve_email_username == True:
+ for email in emails_to_be_filtered:
+ email_parts = email[0].split('@')
+ if len(email_parts[0]) >= 6:
+ email_parts[0] = email_parts[0][0:3] + "[FILTERED]@"
+ self.text = self.text.replace(email[0], email_parts[0] + email_parts[1])
+ else:
+ self.text = self.text.replace(email[0], "[FILTERED]@" + email_parts[1])
+ else:
+ for email in emails_to_be_filtered:
+ self.text = self.text.replace(email[0], re.sub('([a-zA-Z0-9]|_|\.|-){1,201}@',"[FILTERED]@",email[0]))
+
+ self.text = re.sub(self.local_pattern, "[PHONE]", self.text)
+ phones_to_be_filtered = re.findall(self.international_pattern, self.text)
+ if phones_to_be_filtered != ['']:
+ if self.preserve_phone_country_code == True:
+ for phone in phones_to_be_filtered:
+ phone_digits = ''.join(digit for digit in phone[0] if digit.isdigit())
+ code = ""
+ if phone_digits[0] is '0':
+ phone_digits = phone_digits[2:]
+ code = "00"
+ else:
+ code = "+"
+ if len(phone_digits) >= 9:
+ code = code + phone_digits[0:3]
+ elif len(phone_digits) == 8:
+ code = code + phone_digits[0:2]
+ else:
+ code = code + phone_digits[0]
+ self.text = self.text.replace(phone[0], code + " [FILTERED]")
+ else:
+ for phone in phones_to_be_filtered:
+ self.text = self.text.replace(phone[0],"[PHONE]")
+

Божидар обнови решението на 23.04.2014 14:19 (преди над 10 години)

import re
class Validations:
@classmethod
def is_hostname(cls, value):
domain = '([a-zA-Z0-9]{1}([a-zA-Z0-9]|-){0,61}[a-zA-Z0-9]{0,1})'
tld = '(\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,1})$'
pattern = re.compile('^(' + domain + '\.)*' + domain + tld)
return bool(re.search(pattern, value))
@classmethod
def is_email(cls, value):
if bool(re.search('@{1}', value)) is False:
return False
email_pattern = r'^([a-zA-Z0-9]){1}([a-zA-Z0-9]|_|\+|\.|-){0,200}$'
s = value.split('@')
return bool(re.search(email_pattern, s[0])) and cls.is_hostname(s[1])
@classmethod
def is_ip_address(cls, value):
number_range = '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
pattern = re.compile('^(' + number_range + '\.){3}' + number_range + '$')
return bool(re.search(pattern, value))
@classmethod
def is_number(cls, value):
pattern = r'^(-)?(0|[1-9][0-9]*)(\.[0-9]+)?$'
return bool(re.search(pattern, value))
@classmethod
def is_integer(cls, value):
pattern = r'^(-)?(0|[1-9][0-9]*)$'
return bool(re.search(pattern, value))
@classmethod
def is_date(cls, value):
pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$'
return bool(re.search(pattern, value))
@classmethod
def is_time(cls, value):
pattern = r'^([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
return bool(re.search(pattern, value))
@classmethod
def is_datetime (cls, value):
pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])(\s|T|-)([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
return bool(re.search(pattern, value))
@classmethod
def is_phone(cls, value):
interval_pattern = r"((\,|\s|\(|\)){0,2})"
base_digit_pattern = r'(' + interval_pattern + '?[0-9])'
local_pattern = re.compile(r'^0' + interval_pattern + '?[1-9]' + base_digit_pattern + '{5,10}$')
international_pattern = re.compile(r'^(00|'+')'+ \
'?[1-9]' + base_digit_pattern +'{0,2}' + base_digit_pattern + '{6,11}$')
return bool(re.search(local_pattern, value)) or bool(re.search(international_pattern, value))
class PrivacyFilter:
domain = r'(?:[a-zA-Z0-9]{1}(?:[a-zA-Z0-9]|-){0,61}[a-zA-Z0-9]{0,1})'
tld = r'(?:\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,1})'
email_pattern = re.compile(r'(\b(?:[a-zA-Z0-9]){1}(?:[a-zA-Z0-9]|\s|\+|\.|-){0,200}' + '@' + '(?:' + domain + '\.)*' + domain + tld + r'\b)')
interval_pattern = r'(?:(?:\,|\s|\(|\)){0,2})'
base_digit_pattern = r'(?:' + interval_pattern + '?[0-9])'
local_pattern = re.compile(r'0' + interval_pattern + '?[1-9]' + base_digit_pattern + '{5,10}')
international_pattern = re.compile(r'((?:00|\+)'+ \
'?[1-9]' + base_digit_pattern +'{0,2}' + base_digit_pattern + '{6,11})')
def __init__(self, text):
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
self.text = text
def filtered(self):
self.filter()
return self.text
def filter(self):
emails_to_be_filtered = re.findall(self.email_pattern, self.text)
if self.preserve_email_hostname == False and self.partially_preserve_email_username == False:
self.text = re.sub(self.email_pattern, "[EMAIL]", self.text)
elif self.partially_preserve_email_username == True:
for email in emails_to_be_filtered:
email_parts = email[0].split('@')
if len(email_parts[0]) >= 6:
email_parts[0] = email_parts[0][0:3] + "[FILTERED]@"
self.text = self.text.replace(email[0], email_parts[0] + email_parts[1])
else:
self.text = self.text.replace(email[0], "[FILTERED]@" + email_parts[1])
else:
for email in emails_to_be_filtered:
self.text = self.text.replace(email[0], re.sub('([a-zA-Z0-9]|_|\.|-){1,201}@',"[FILTERED]@",email[0]))
self.text = re.sub(self.local_pattern, "[PHONE]", self.text)
phones_to_be_filtered = re.findall(self.international_pattern, self.text)
if phones_to_be_filtered != ['']:
if self.preserve_phone_country_code == True:
for phone in phones_to_be_filtered:
phone_digits = ''.join(digit for digit in phone[0] if digit.isdigit())
code = ""
if phone_digits[0] is '0':
phone_digits = phone_digits[2:]
code = "00"
else:
code = "+"
if len(phone_digits) >= 9:
code = code + phone_digits[0:3]
elif len(phone_digits) == 8:
code = code + phone_digits[0:2]
else:
code = code + phone_digits[0]
self.text = self.text.replace(phone[0], code + " [FILTERED]")
else:
for phone in phones_to_be_filtered:
- self.text = self.text.replace(phone[0],"[PHONE]")
-
+ self.text = self.text.replace(phone[0],"[PHONE]")

Божидар обнови решението на 23.04.2014 14:48 (преди над 10 години)

import re
class Validations:
@classmethod
def is_hostname(cls, value):
domain = '([a-zA-Z0-9]{1}([a-zA-Z0-9]|-){0,61}[a-zA-Z0-9]{0,1})'
tld = '(\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,1})$'
pattern = re.compile('^(' + domain + '\.)*' + domain + tld)
return bool(re.search(pattern, value))
@classmethod
def is_email(cls, value):
if bool(re.search('@{1}', value)) is False:
return False
email_pattern = r'^([a-zA-Z0-9]){1}([a-zA-Z0-9]|_|\+|\.|-){0,200}$'
s = value.split('@')
return bool(re.search(email_pattern, s[0])) and cls.is_hostname(s[1])
@classmethod
def is_ip_address(cls, value):
number_range = '([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])'
pattern = re.compile('^(' + number_range + '\.){3}' + number_range + '$')
return bool(re.search(pattern, value))
@classmethod
def is_number(cls, value):
pattern = r'^(-)?(0|[1-9][0-9]*)(\.[0-9]+)?$'
return bool(re.search(pattern, value))
@classmethod
def is_integer(cls, value):
pattern = r'^(-)?(0|[1-9][0-9]*)$'
return bool(re.search(pattern, value))
@classmethod
def is_date(cls, value):
pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$'
return bool(re.search(pattern, value))
@classmethod
def is_time(cls, value):
pattern = r'^([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
return bool(re.search(pattern, value))
@classmethod
- def is_datetime (cls, value):
+ def is_datetime(cls, value):
pattern = r'^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])(\s|T|-)([0-1][0-9]|2[0-3])(:[0-5][0-9]){2}$'
return bool(re.search(pattern, value))
@classmethod
def is_phone(cls, value):
interval_pattern = r"((\,|\s|\(|\)){0,2})"
base_digit_pattern = r'(' + interval_pattern + '?[0-9])'
local_pattern = re.compile(r'^0' + interval_pattern + '?[1-9]' + base_digit_pattern + '{5,10}$')
- international_pattern = re.compile(r'^(00|'+')'+ \
- '?[1-9]' + base_digit_pattern +'{0,2}' + base_digit_pattern + '{6,11}$')
+ international_pattern = re.compile(r'^(00|\+)' + '?[1-9][0-9]{0,2}' + base_digit_pattern + '{6,11}$')
return bool(re.search(local_pattern, value)) or bool(re.search(international_pattern, value))
class PrivacyFilter:
domain = r'(?:[a-zA-Z0-9]{1}(?:[a-zA-Z0-9]|-){0,61}[a-zA-Z0-9]{0,1})'
tld = r'(?:\.[a-zA-Z]{2,3}(\.[a-zA-Z]{2}){0,1})'
email_pattern = re.compile(r'(\b(?:[a-zA-Z0-9]){1}(?:[a-zA-Z0-9]|\s|\+|\.|-){0,200}' + '@' + '(?:' + domain + '\.)*' + domain + tld + r'\b)')
interval_pattern = r'(?:(?:\,|\s|\(|\)){0,2})'
base_digit_pattern = r'(?:' + interval_pattern + '?[0-9])'
local_pattern = re.compile(r'0' + interval_pattern + '?[1-9]' + base_digit_pattern + '{5,10}')
- international_pattern = re.compile(r'((?:00|\+)'+ \
- '?[1-9]' + base_digit_pattern +'{0,2}' + base_digit_pattern + '{6,11})')
+ international_pattern = re.compile(r'((?:00|\+)' + '?[1-9][0-9]{0,2}' + base_digit_pattern + '{6,11})')
def __init__(self, text):
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
self.text = text
def filtered(self):
self.filter()
return self.text
def filter(self):
emails_to_be_filtered = re.findall(self.email_pattern, self.text)
if self.preserve_email_hostname == False and self.partially_preserve_email_username == False:
self.text = re.sub(self.email_pattern, "[EMAIL]", self.text)
elif self.partially_preserve_email_username == True:
for email in emails_to_be_filtered:
email_parts = email[0].split('@')
if len(email_parts[0]) >= 6:
email_parts[0] = email_parts[0][0:3] + "[FILTERED]@"
self.text = self.text.replace(email[0], email_parts[0] + email_parts[1])
else:
self.text = self.text.replace(email[0], "[FILTERED]@" + email_parts[1])
else:
for email in emails_to_be_filtered:
self.text = self.text.replace(email[0], re.sub('([a-zA-Z0-9]|_|\.|-){1,201}@',"[FILTERED]@",email[0]))
self.text = re.sub(self.local_pattern, "[PHONE]", self.text)
phones_to_be_filtered = re.findall(self.international_pattern, self.text)
if phones_to_be_filtered != ['']:
if self.preserve_phone_country_code == True:
for phone in phones_to_be_filtered:
phone_digits = ''.join(digit for digit in phone[0] if digit.isdigit())
code = ""
if phone_digits[0] is '0':
phone_digits = phone_digits[2:]
code = "00"
else:
code = "+"
if len(phone_digits) >= 9:
code = code + phone_digits[0:3]
elif len(phone_digits) == 8:
code = code + phone_digits[0:2]
else:
code = code + phone_digits[0]
self.text = self.text.replace(phone[0], code + " [FILTERED]")
else:
for phone in phones_to_be_filtered:
self.text = self.text.replace(phone[0],"[PHONE]")