Решение на Регулярни изрази от Георги Антонов

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

Към профила на Георги Антонов

Резултати

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

Код

import re
class PrivacyFilter:
def __init__(self, text):
self.originalText = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
internationalPhonePattern = r'((?:\+|\b00)[1-9]\d{0,2})[\(\) -]{0,2}((?:[\(\) -]{0,2}\d){6,11})\b'
localPhonePattern = r'0[\(\) -]{0,2}[1-9][\(\) -]{0,2}((?:[\(\) -]{0,2}\d){5,11})\b'
hostNamePattern = r'((?:(?:[0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2,3})?)'
emailWithShortUsernamePattern = r'\b[a-zA-Z0-9][a-zA-Z0-9_+.-]{0,5}@' + hostNamePattern
emailWithLongUsernamePattern = r'\b([a-zA-Z0-9][a-zA-Z0-9_+.-]{2})[a-zA-Z0-9_+.-]{0,198}@' + hostNamePattern
if self.preserve_phone_country_code:
text = re.sub(internationalPhonePattern, r'\1 [FILTERED]', self.originalText)
else:
text = re.sub(internationalPhonePattern, '[PHONE]', self.originalText)
text = re.sub(localPhonePattern, '[PHONE]', text)
if self.partially_preserve_email_username:
text = re.sub(emailWithLongUsernamePattern, r'\1[FILTERED]@\2', text)
text = re.sub(emailWithShortUsernamePattern, r'[FILTERED]@\1', text)
elif self.preserve_email_hostname:
text = re.sub(emailWithLongUsernamePattern, r'[FILTERED]@\2', text)
text = re.sub(emailWithShortUsernamePattern, r'[FILTERED]@\1', text)
else:
text = re.sub(emailWithShortUsernamePattern, '[EMAIL]', text)
text = re.sub(emailWithLongUsernamePattern, '[EMAIL]', text)
return text
class Validations:
hostNamePattern = r'(?:(?:[0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2,3})?'
emailPattern = r'[a-zA-Z0-9][a-zA-Z0-9_+.-]{0,200}@' + hostNamePattern
phonePattern = r'(?:(?:\+|\b00)[1-9]\d{0,2}|0)[\(\) -]{0,2}[1-9][\(\) -]{0,2}(?:[\(\) -]{0,2}\d){5,11}'
ipAddressPattern = r'(?:[12]?[1-9])?\d(?:\.(?:[12]?[1-9])?\d){3}'
intPattern = r'-?(?:0|[1-9]\d*)'
floatPattern = intPattern + r'(?:.\d+)?'
datePattern = r'\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])'
timePattern = r'(?:[01]\d|2[0-3])(?::[0-5]\d){2}'
dateTimePattern = datePattern + '[T ]' + timePattern
@classmethod
def is_email(cls, value):
return re.match('^' + cls.emailPattern + '$', value) is not None;
@classmethod
def is_hostname(cls, value):
return re.match('^' + cls.hostNamePattern + '$', value) is not None;
@classmethod
def is_phone(cls, value):
return re.match('^' + cls.phonePattern + '$', value) is not None;
@classmethod
def is_ip_address(cls, value):
return re.match('^' + cls.ipAddressPattern + '$', value) is not None;
@classmethod
def is_number(cls, value):
return re.match('^' + cls.floatPattern + '$', value) is not None;
@classmethod
def is_integer(cls, value):
return re.match('^' + cls.intPattern + '$', value) is not None;
@classmethod
def is_date(cls, value):
return re.match('^' + cls.datePattern + '$', value) is not None;
@classmethod
def is_time(cls, value):
return re.match('^' + cls.timePattern + '$', value) is not None;
@classmethod
def is_datetime(cls, value):
return re.match('^' + cls.dateTimePattern + '$', value) is not None;

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

...FF..F........F.....FF..FF...........
======================================================================
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-1451j9j/test.py", line 48, in test_does_not_filter_invalid_emails
    self.assertEqual(text, solution.PrivacyFilter(text).filtered())
AssertionError: 'And more: someone@invalid.domaintld' != 'And more: [EMAIL]aintld'
- And more: someone@invalid.domaintld
+ And more: [EMAIL]aintld


======================================================================
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-1451j9j/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '0005551234569' != '00[PHONE]'
- 0005551234569
+ 00[PHONE]


======================================================================
FAIL: test_obfuscates_more_complicated_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-1451j9j/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact: [EMAIL],[EMAIL]' != 'Contact: [EMAIL],someone.[EMAIL]'
- Contact: [EMAIL],[EMAIL]
+ Contact: [EMAIL],someone.[EMAIL]
?                  ++++++++


======================================================================
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-1451j9j/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-1451j9j/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-1451j9j/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-1451j9j/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-1451j9j/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.050s

FAILED (failures=8)

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

Георги обнови решението на 23.04.2014 01:20 (преди над 10 години)

+import re
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.originalText = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ internationalPhonePattern = r'((?:\+|\b00)[1-9]\d{0,2})[\(\) -]{0,2}((?:[\(\) -]{0,2}\d){6,11})\b'
+ localPhonePattern = r'0[\(\) -]{0,2}[1-9][\(\) -]{0,2}((?:[\(\) -]{0,2}\d){5,11})\b'
+ hostNamePattern = r'((?:(?:[0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.)*[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2,3})?)'
+ emailWithShortUsernamePattern = r'\b[a-zA-Z0-9][a-zA-Z0-9_+.-]{0,5}@' + hostNamePattern
+ emailWithLongUsernamePattern = r'\b([a-zA-Z0-9][a-zA-Z0-9_+.-]{2})[a-zA-Z0-9_+.-]{0,198}@' + hostNamePattern
+
+ if self.preserve_phone_country_code:
+ text = re.sub(internationalPhonePattern, r'\1 [FILTERED]', self.originalText)
+ else:
+ text = re.sub(internationalPhonePattern, '[PHONE]', self.originalText)
+ text = re.sub(localPhonePattern, '[PHONE]', text)
+
+ if self.partially_preserve_email_username:
+ text = re.sub(emailWithLongUsernamePattern, r'\1[FILTERED]@\2', text)
+ text = re.sub(emailWithShortUsernamePattern, r'[FILTERED]@\1', text)
+ elif self.preserve_email_hostname:
+ text = re.sub(emailWithLongUsernamePattern, r'[FILTERED]@\2', text)
+ text = re.sub(emailWithShortUsernamePattern, r'[FILTERED]@\1', text)
+ else:
+ text = re.sub(emailWithShortUsernamePattern, '[EMAIL]', text)
+ text = re.sub(emailWithLongUsernamePattern, '[EMAIL]', text)
+ return text

Георги обнови решението на 23.04.2014 02:25 (преди над 10 години)

import re
class PrivacyFilter:
def __init__(self, text):
self.originalText = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
internationalPhonePattern = r'((?:\+|\b00)[1-9]\d{0,2})[\(\) -]{0,2}((?:[\(\) -]{0,2}\d){6,11})\b'
localPhonePattern = r'0[\(\) -]{0,2}[1-9][\(\) -]{0,2}((?:[\(\) -]{0,2}\d){5,11})\b'
- hostNamePattern = r'((?:(?:[0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.)*[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2,3})?)'
+ hostNamePattern = r'((?:(?:[0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2,3})?)'
emailWithShortUsernamePattern = r'\b[a-zA-Z0-9][a-zA-Z0-9_+.-]{0,5}@' + hostNamePattern
emailWithLongUsernamePattern = r'\b([a-zA-Z0-9][a-zA-Z0-9_+.-]{2})[a-zA-Z0-9_+.-]{0,198}@' + hostNamePattern
if self.preserve_phone_country_code:
text = re.sub(internationalPhonePattern, r'\1 [FILTERED]', self.originalText)
else:
text = re.sub(internationalPhonePattern, '[PHONE]', self.originalText)
text = re.sub(localPhonePattern, '[PHONE]', text)
if self.partially_preserve_email_username:
text = re.sub(emailWithLongUsernamePattern, r'\1[FILTERED]@\2', text)
text = re.sub(emailWithShortUsernamePattern, r'[FILTERED]@\1', text)
elif self.preserve_email_hostname:
text = re.sub(emailWithLongUsernamePattern, r'[FILTERED]@\2', text)
text = re.sub(emailWithShortUsernamePattern, r'[FILTERED]@\1', text)
else:
text = re.sub(emailWithShortUsernamePattern, '[EMAIL]', text)
text = re.sub(emailWithLongUsernamePattern, '[EMAIL]', text)
- return text
+ return text
+
+class Validations:
+ hostNamePattern = r'(?:(?:[0-9a-zA-Z][0-9a-zA-Z-]{0,61})?[0-9a-zA-Z]\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2,3})?'
+ emailPattern = r'[a-zA-Z0-9][a-zA-Z0-9_+.-]{0,200}@' + hostNamePattern
+ phonePattern = r'(?:(?:\+|\b00)[1-9]\d{0,2}|0)[\(\) -]{0,2}[1-9][\(\) -]{0,2}(?:[\(\) -]{0,2}\d){5,11}'
+ ipAddressPattern = r'(?:[12]?[1-9])?\d(?:\.(?:[12]?[1-9])?\d){3}'
+ intPattern = r'-?(?:0|[1-9]\d*)'
+ floatPattern = intPattern + r'(?:.\d+)?'
+ datePattern = r'\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])'
+ timePattern = r'(?:[01]\d|2[0-3])(?::[0-5]\d){2}'
+ dateTimePattern = datePattern + '[T ]' + timePattern
+
+ @classmethod
+ def is_email(cls, value):
+ return re.match('^' + cls.emailPattern + '$', value) is not None;
+ @classmethod
+ def is_hostname(cls, value):
+ return re.match('^' + cls.hostNamePattern + '$', value) is not None;
+ @classmethod
+ def is_phone(cls, value):
+ return re.match('^' + cls.phonePattern + '$', value) is not None;
+ @classmethod
+ def is_ip_address(cls, value):
+ return re.match('^' + cls.ipAddressPattern + '$', value) is not None;
+ @classmethod
+ def is_number(cls, value):
+ return re.match('^' + cls.floatPattern + '$', value) is not None;
+ @classmethod
+ def is_integer(cls, value):
+ return re.match('^' + cls.intPattern + '$', value) is not None;
+ @classmethod
+ def is_date(cls, value):
+ return re.match('^' + cls.datePattern + '$', value) is not None;
+ @classmethod
+ def is_time(cls, value):
+ return re.match('^' + cls.timePattern + '$', value) is not None;
+ @classmethod
+ def is_datetime(cls, value):
+ return re.match('^' + cls.dateTimePattern + '$', value) is not None;