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

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

Към профила на Радослав Георгиев

Резултати

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

Код

import re
class PrivacyFilter:
REGEX_EMAIL = re.compile(r'\b([a-zA-Z0-9][a-zA-Z0-9_+.-]{0,200})@((?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?)\b')
REGEX_PHONE = re.compile(r'(\b0(?!0)|(?:\b00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}\b')
REGEX_PHONE_INTL = re.compile(r'^(?:(?:00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}$')
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):
filtered_text = self.text
# First we obfuscate the emails...:
if self.partially_preserve_email_username:
matches = PrivacyFilter.REGEX_EMAIL.finditer(filtered_text)
for match in matches:
username = match.group(1)
if len(username) >= 6:
filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'{}[FILTERED]@\2'.format(username[:3]), filtered_text, 1)
else:
filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[FILTERED]@\2', filtered_text, 1)
elif self.preserve_email_hostname:
filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[FILTERED]@\2', filtered_text)
else:
filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[EMAIL]', filtered_text)
# ...and then we obfuscate the phone numbers:
if self.preserve_phone_country_code:
matches = PrivacyFilter.REGEX_PHONE.finditer(filtered_text)
for match in matches:
if PrivacyFilter.REGEX_PHONE_INTL.match(match.group()): # ...if the phone number is international...
filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'\1 [FILTERED]', filtered_text, 1)
else:
filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'[PHONE]', filtered_text, 1)
else:
filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'[PHONE]', filtered_text)
return filtered_text
class Validations:
REGEX_EMAIL = re.compile(r'^(?:[a-zA-Z0-9][a-zA-Z0-9_+.-]{0,200})@(?:(?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?)$')
REGEX_PHONE = re.compile(r'^(?:0(?!0)|(?:00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}$')
REGEX_HOSTNAME = re.compile(r'^(?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?$')
REGEX_IP_ADDRESS = re.compile(r'^(?:(?:0|[1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:0|[1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])$')
REGEX_NUMBER = re.compile(r'^-?(?:0|[1-9]\d*)(?:\.\d+)?$')
REGEX_INTEGER = re.compile(r'^-?(?:0|[1-9]\d*)$')
REGEX_DATE = re.compile(r'^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$')
REGEX_TIME = re.compile(r'^(?:[01]\d|2[0-3])(?::[0-5]\d){2}$')
REGEX_DATETIME = re.compile(r'^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])[ T](?:[01]\d|2[0-3])(?::[0-5]\d){2}$')
@classmethod
def is_email(cls, value):
return bool(cls.REGEX_EMAIL.match(value))
@classmethod
def is_phone(cls, value):
return bool(cls.REGEX_PHONE.match(value))
@classmethod
def is_hostname(cls, value):
return bool(cls.REGEX_HOSTNAME.match(value))
@classmethod
def is_ip_address(cls, value):
return bool(cls.REGEX_IP_ADDRESS.match(value))
@classmethod
def is_number(cls, value):
return bool(cls.REGEX_NUMBER.match(value))
@classmethod
def is_integer(cls, value):
return bool(cls.REGEX_INTEGER.match(value))
@classmethod
def is_date(cls, value):
return bool(cls.REGEX_DATE.match(value))
@classmethod
def is_time(cls, value):
return bool(cls.REGEX_TIME.match(value))
@classmethod
def is_datetime(cls, value):
return bool(cls.REGEX_DATETIME.match(value))

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

....F...........F.....FF..FF....F......
======================================================================
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-up24b2/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-up24b2/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-up24b2/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-up24b2/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-up24b2/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-up24b2/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-up24b2/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.024s

FAILED (failures=7)

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

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

+import re
+
+
+class PrivacyFilter:
+ REGEX_EMAIL = re.compile(r'\b([a-zA-Z0-9][a-zA-Z0-9_+.-]{0,200})@((?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?)\b')
+ REGEX_PHONE = re.compile(r'(\b0(?!0)|(?:\b00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}\b')
+ REGEX_PHONE_INTL = re.compile(r'^(?:(?:00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}$')
+
+ 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):
+ filtered_text = self.text
+
+ # First we obfuscate the emails...:
+
+ if self.partially_preserve_email_username:
+ matches = PrivacyFilter.REGEX_EMAIL.finditer(filtered_text)
+
+ for match in matches:
+ username = match.group(1)
+
+ if len(username) >= 6:
+ filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'{}[FILTERED]@\2'.format(username[:3]), filtered_text, 1)
+ else:
+ filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[FILTERED]@\2', filtered_text, 1)
+ elif self.preserve_email_hostname:
+ filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[FILTERED]@\2', filtered_text)
+ else:
+ filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[EMAIL]', filtered_text)
+
+ # ...and then we obfuscate the phone numbers:
+
+ if self.preserve_phone_country_code:
+ matches = PrivacyFilter.REGEX_PHONE.finditer(filtered_text)
+
+ for match in matches:
+ if PrivacyFilter.REGEX_PHONE_INTL.match(match.group()): # ...if the phone number is international...
+ filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'\1 [FILTERED]', filtered_text, 1)
+ else:
+ filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'[PHONE]', filtered_text, 1)
+ else:
+ filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'[PHONE]', filtered_text)
+
+ return filtered_text
+
+
+class Validations:
+ REGEX_EMAIL = re.compile(r'^(?:[a-zA-Z0-9][a-zA-Z0-9_+.-]{0,200})@(?:(?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?)$')
+ REGEX_PHONE = re.compile(r'^(?:0(?!0)|(?:00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}$')
+ REGEX_HOSTNAME = re.compile(r'^(?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?$')
+ REGEX_IP_ADDRESS = re.compile(r'^(?:(?:0|[1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:0|[1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])$')
+ REGEX_NUMBER = re.compile(r'^-?(?:0|[1-9]\d*)(?:\.\d+)?$')
+ REGEX_INTEGER = re.compile(r'^-?(?:0|[1-9]\d*)$')
+ REGEX_DATE = re.compile(r'^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$')
+ REGEX_TIME = re.compile(r'^(?:[01]\d|2[0-3])(?::[0-5]\d){2}$')
+ REGEX_DATETIME = re.compile(r'^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])[ T](?:[01]\d|2[0-3])(?::[0-5]\d){2}$')
+
+ @classmethod
+ def is_email(cls, value):
+ return bool(cls.REGEX_EMAIL.match(value))
+
+ @classmethod
+ def is_phone(cls, value):
+ return bool(cls.REGEX_PHONE.match(value))
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(cls.REGEX_HOSTNAME.match(value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(cls.REGEX_IP_ADDRESS.match(value))
+
+ @classmethod
+ def is_number(cls, value):
+ return bool(cls.REGEX_NUMBER.match(value))
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(cls.REGEX_INTEGER.match(value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(cls.REGEX_DATE.match(value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(cls.REGEX_TIME.match(value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ return bool(cls.REGEX_DATETIME.match(value))

Радослав обнови решението на 23.04.2014 04:28 (преди над 10 години)

import re
class PrivacyFilter:
REGEX_EMAIL = re.compile(r'\b([a-zA-Z0-9][a-zA-Z0-9_+.-]{0,200})@((?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?)\b')
REGEX_PHONE = re.compile(r'(\b0(?!0)|(?:\b00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}\b')
REGEX_PHONE_INTL = re.compile(r'^(?:(?:00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}$')
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):
filtered_text = self.text
# First we obfuscate the emails...:
if self.partially_preserve_email_username:
matches = PrivacyFilter.REGEX_EMAIL.finditer(filtered_text)
for match in matches:
username = match.group(1)
if len(username) >= 6:
filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'{}[FILTERED]@\2'.format(username[:3]), filtered_text, 1)
else:
filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[FILTERED]@\2', filtered_text, 1)
elif self.preserve_email_hostname:
filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[FILTERED]@\2', filtered_text)
else:
filtered_text = PrivacyFilter.REGEX_EMAIL.sub(r'[EMAIL]', filtered_text)
# ...and then we obfuscate the phone numbers:
if self.preserve_phone_country_code:
matches = PrivacyFilter.REGEX_PHONE.finditer(filtered_text)
for match in matches:
- if PrivacyFilter.REGEX_PHONE_INTL.match(match.group()): # ...if the phone number is international...
+ if PrivacyFilter.REGEX_PHONE_INTL.match(match.group()): # ...if the phone number is international...
filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'\1 [FILTERED]', filtered_text, 1)
else:
filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'[PHONE]', filtered_text, 1)
else:
filtered_text = PrivacyFilter.REGEX_PHONE.sub(r'[PHONE]', filtered_text)
return filtered_text
class Validations:
REGEX_EMAIL = re.compile(r'^(?:[a-zA-Z0-9][a-zA-Z0-9_+.-]{0,200})@(?:(?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?)$')
REGEX_PHONE = re.compile(r'^(?:0(?!0)|(?:00|\+)[1-9]\d{0,2})(?:[ ()-]{0,2}\d){6,11}$')
REGEX_HOSTNAME = re.compile(r'^(?:[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9]?\.)+[a-zA-Z]{2,3}(?:\.[a-zA-Z]{2})?$')
REGEX_IP_ADDRESS = re.compile(r'^(?:(?:0|[1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])\.){3}(?:0|[1-9]\d?|1\d{2}|2[0-4]\d|25[0-5])$')
REGEX_NUMBER = re.compile(r'^-?(?:0|[1-9]\d*)(?:\.\d+)?$')
REGEX_INTEGER = re.compile(r'^-?(?:0|[1-9]\d*)$')
REGEX_DATE = re.compile(r'^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$')
REGEX_TIME = re.compile(r'^(?:[01]\d|2[0-3])(?::[0-5]\d){2}$')
REGEX_DATETIME = re.compile(r'^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])[ T](?:[01]\d|2[0-3])(?::[0-5]\d){2}$')
@classmethod
def is_email(cls, value):
return bool(cls.REGEX_EMAIL.match(value))
@classmethod
def is_phone(cls, value):
return bool(cls.REGEX_PHONE.match(value))
@classmethod
def is_hostname(cls, value):
return bool(cls.REGEX_HOSTNAME.match(value))
@classmethod
def is_ip_address(cls, value):
return bool(cls.REGEX_IP_ADDRESS.match(value))
@classmethod
def is_number(cls, value):
return bool(cls.REGEX_NUMBER.match(value))
@classmethod
def is_integer(cls, value):
return bool(cls.REGEX_INTEGER.match(value))
@classmethod
def is_date(cls, value):
return bool(cls.REGEX_DATE.match(value))
@classmethod
def is_time(cls, value):
return bool(cls.REGEX_TIME.match(value))
@classmethod
def is_datetime(cls, value):
return bool(cls.REGEX_DATETIME.match(value))