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

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

Към профила на Васил Тодоров

Резултати

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

Код

import re
PATTERN_DOM = r'(\w(([\w-]{0,61}?)\w)?\.)'
PATTERN_TLD = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?'
PATTERN_HOSTNAME = PATTERN_DOM + r'+' + PATTERN_TLD
PATTERN_EMAIL = r'[\w][\w_\-\.\+]{0,200}@(' + PATTERN_HOSTNAME + r'\b' + ')'
PATTERN_PHONE = r'(((\+|00)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){5,10}'
PATTERN_DATE = r'\d{4}-(02-([01]\d|2[1-8])|(01|03|05|07|08|10|12)'
PATTERN_DATE += r'-([012]\d|3[01])|(04|06|09|11)-([012]\d|30))'
PATTERN_HOUR = r'(2[0-3]|[01]\d|)'
PATTERN_MINUTE = r'[0-5]\d'
PATTERN_TIME = PATTERN_HOUR + ":" + PATTERN_MINUTE + ":" + PATTERN_MINUTE
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):
new_text = self.text
pattern_short_email = r'\b[\w][\w_\-\.\+]{0,5}@'
pattern_short_email += '(' + PATTERN_HOSTNAME + r'\b' + ')'
pattern_long_mail = r'\b([\w][\w_\-\.\+]{2})[\w_\-\.\+]+@'
pattern_long_mail += r'(' + PATTERN_HOSTNAME + r'\b' + ')'
international = r'((\+|00)[1-9]\d{0,2})[-\(\)\s]{0,2}[1-9]([-\(\)\s]{0,2}\d){5,10}\b'
country = r'\b0([-\(\)\s]{0,2}\d){6,11}\b'
if not self.preserve_email_hostname and not self.partially_preserve_email_username:
new_text = re.sub(r'\b' + PATTERN_EMAIL, '[EMAIL]', new_text)
elif self.preserve_email_hostname and not self.partially_preserve_email_username:
new_text = re.sub(r'\b' + PATTERN_EMAIL, '[FILTERED]' + '@' + '\\1', new_text)
else:
new_text = re.sub(pattern_short_email, '[FILTERED]@' + '\\1', new_text)
new_text = re.sub(pattern_long_mail, '\\1' + '[FILTERED]@' + '\\2', new_text)
if not self.preserve_phone_country_code:
new_text = re.sub(PATTERN_PHONE, '[PHONE]', new_text)
else:
new_text = re.sub(international, '\\1' + ' [FILTERED]', new_text)
new_text = re.sub(country, '[FILTERED]', new_text)
return new_text
class Validations:
def is_email(value):
return(bool(re.match(r'^' + PATTERN_EMAIL + r'$', value)))
def is_phone(value):
return bool(re.match(r'^' + PATTERN_PHONE + r'$', value))
def is_hostname(value):
return bool(re.match(r'^' + PATTERN_HOSTNAME + r'$', value))
def is_ip_address(value):
pattern_8bit_num = r'(1?[1-9]?\d|2[0-4]\d|25[0-5])'
pattern_ip_address = r'^' + (pattern_8bit_num + '\.') * 3
pattern_ip_address += pattern_8bit_num + '$'
return bool(re.match(pattern_ip_address, value))
def is_number(value):
pattern_number = r'^-?(0|[1-9]\d*)([\.,]\d+)?$'
return bool(re.match(pattern_number, value))
def is_integer(value):
pattern_integer = r'^-?(0|[1-9]\d*)$'
return bool(re.match(pattern_integer, value))
def is_date(value):
return bool(re.match(r'^' + PATTERN_DATE + r'$', value))
def is_time(value):
return bool(re.match(r'^' + PATTERN_TIME + '$', value))
def is_datetime(value):
pattern_datetime = r'^' + PATTERN_DATE + r'\s' + PATTERN_TIME + '$'
return bool(re.match(pattern_datetime, value))

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

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


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


======================================================================
FAIL: test_can_validate_more_complex_emails (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-olax1x/test.py", line 124, in test_can_validate_more_complex_emails
    self.assertIs(solution.Validations.is_email(email), valid)
AssertionError: True is not False

======================================================================
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-olax1x/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_does_not_allow_zero_months_or_days_in_dates (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-olax1x/test.py", line 250, in test_does_not_allow_zero_months_or_days_in_dates
    self.assertFalse(solution.Validations.is_date('1000-01-00'))
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-olax1x/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-olax1x/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-olax1x/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-olax1x/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_datetime_values (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-olax1x/test.py", line 278, in test_validates_datetime_values
    self.assertTrue(solution.Validations.is_datetime('2012-11-19T19:00:00'))
AssertionError: False is not true

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

FAILED (failures=11)

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

Васил обнови решението на 21.04.2014 00:38 (преди над 10 години)

+import re
+
+
+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):
+ newText = self.text
+ pattern_dom = r'(\w(([\w-]{0,61}?)\w)?\.)'
+ pattern_tld = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?\b'
+ pattern_hostname = pattern_dom + r'+' + pattern_tld + r'\b'
+ pattern_email = r'\b[\w][\w_\-\.\+]{0,200}@'
+ pattern_email += '(' + pattern_hostname + ')'
+ phone = r'(((\+|00)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){5,10}'
+ if not self.preserve_email_hostname and not self.partially_preserve_email_username:
+ newText = re.sub(pattern_email, '[EMAIL]', newText)
+ elif self.preserve_email_hostname and not self.partially_preserve_email_username:
+ newText = re.sub(pattern_email, '[FILTERED]' + '@' + '\\1', newText)
+ else:
+ pattern_short_email = r'\b[\w][\w_\-\.\+]{0,5}@'
+ pattern_short_email += '(' + pattern_hostname + ')'
+ newText = re.sub(pattern_short_email, '[FILTERED]@' + '\\1', newText)
+ pattern_long_mail = r'\b([\w][\w_\-\.\+]{2})[\w_\-\.\+]+@'
+ pattern_long_mail += r'(' + pattern_hostname + ')'
+ newText = re.sub(pattern_long_mail, '\\1' + '[FILTERED]@' + '\\2', newText)
+
+ if not self.preserve_phone_country_code:
+ newText = re.sub(phone, '[PHONE]', newText)
+ else:
+ international = r'((\+|00)[1-9]\d{0,2})[-\(\)\s]{0,2}[1-9]([-\(\)\s]{0,2}\d){5,10}\b'
+ newText = re.sub(international, '\\1' + ' [FILTERED]', newText)
+ country = r'\b0([-\(\)\s]{0,2}\d){6,11}\b'
+ newText = re.sub(country, '[FILTERED]', newText)
+ return newText
+
+
+class Validations:
+ def is_email(value):
+ pattern_dom = r'(\w(([\w-]{0,61}?)\w)?\.)'
+ pattern_tld = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?'
+ pattern_hostname = pattern_dom + r'+' + pattern_tld + r'$'
+ pattern = r'^[\w][\w_\-\.\+]{0,200}@'
+ pattern += pattern_hostname
+ return(bool(re.match(pattern, value)))
+
+ def is_phone(value):
+ pattern = r'^(((\+|00)[1-9]\d{0,2})|0)[-\(\)\s]{0,2}[1-9]'
+ pattern += r'([-\(\)\s]{0,2}\d){5,10}$'
+ return bool(re.match(pattern, value))
+
+ def is_hostname(value):
+ pattern_dom = r'(\w(([\w-]{0,61}?)\w)?\.)'
+ pattern_tld = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?'
+ pattern = r'^' + pattern_dom + r'+' + pattern_tld + r'$'
+ return bool(re.match(pattern, value))
+
+ def is_ip_address(value):
+ matchNum = r'(1?[1-9]?\d|2[0-4]\d|25[0-5])'
+ pattern = r'^' + (matchNum + '\.') * 3 + matchNum + '$'
+ return bool(re.match(pattern, value))
+
+ def is_number(value):
+ pattern = r'^-?(0|[1-9]\d*)([\.,]\d+)?$'
+ return bool(re.match(pattern, value))
+
+ def is_integer(value):
+ pattern = r'^-?(0|[1-9]\d*)$'
+ return bool(re.match(pattern, value))
+
+ def is_date(value):
+ pattern = r'^\d{4}-(02-([01]\d|2[1-8])|(01|03|05|07|08|10|12)'
+ pattern += r'-([012]\d|3[01])|(04|06|09|11)-([012]\d|30))$'
+ return bool(re.match(pattern, value))
+
+ def is_time(value):
+ hour_patt = r'(2[0-3]|[01]\d|)'
+ min_patt = r'[0-5]\d'
+ pattern = r'^' + hour_patt + ":" + min_patt + ":" + min_patt + '$'
+ return bool(re.match(pattern, value))
+
+ def is_datetime(value):
+ date_pattern = r'^\d{4}-(02-([01]\d|2[1-8])|(01|03|05|07|08|10|12)'
+ date_pattern += r'-([012]\d|3[01])|(04|06|09|11)-([012]\d|30))'
+ hour_patt = r'(2[0-3]|[01]\d|)'
+ min_patt = r'[0-5]\d'
+ time_pattern = hour_patt + ":" + min_patt + ":" + min_patt + '$'
+ return bool(re.match(date_pattern + r'\s' + time_pattern, value))

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

import re
+PATTERN_DOM = r'(\w(([\w-]{0,61}?)\w)?\.)'
+PATTERN_TLD = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?'
+PATTERN_HOSTNAME = PATTERN_DOM + r'+' + PATTERN_TLD
+PATTERN_EMAIL = r'[\w][\w_\-\.\+]{0,200}@(' + PATTERN_HOSTNAME + r'\b' + ')'
+PATTERN_PHONE = r'(((\+|00)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){5,10}'
+PATTERN_DATE = r'\d{4}-(02-([01]\d|2[1-8])|(01|03|05|07|08|10|12)'
+PATTERN_DATE += r'-([012]\d|3[01])|(04|06|09|11)-([012]\d|30))'
+PATTERN_HOUR = r'(2[0-3]|[01]\d|)'
+PATTERN_MINUTE = r'[0-5]\d'
+PATTERN_TIME = PATTERN_HOUR + ":" + PATTERN_MINUTE + ":" + PATTERN_MINUTE
+
+
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):
- newText = self.text
- pattern_dom = r'(\w(([\w-]{0,61}?)\w)?\.)'
- pattern_tld = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?\b'
- pattern_hostname = pattern_dom + r'+' + pattern_tld + r'\b'
- pattern_email = r'\b[\w][\w_\-\.\+]{0,200}@'
- pattern_email += '(' + pattern_hostname + ')'
- phone = r'(((\+|00)[1-9]\d{0,2})|0)([-\(\)\s]{0,2}\d){5,10}'
+ new_text = self.text
+ pattern_short_email = r'\b[\w][\w_\-\.\+]{0,5}@'
+ pattern_short_email += '(' + PATTERN_HOSTNAME + r'\b' + ')'
+ pattern_long_mail = r'\b([\w][\w_\-\.\+]{2})[\w_\-\.\+]+@'
+ pattern_long_mail += r'(' + PATTERN_HOSTNAME + r'\b' + ')'
+ international = r'((\+|00)[1-9]\d{0,2})[-\(\)\s]{0,2}[1-9]([-\(\)\s]{0,2}\d){5,10}\b'
+ country = r'\b0([-\(\)\s]{0,2}\d){6,11}\b'
if not self.preserve_email_hostname and not self.partially_preserve_email_username:
- newText = re.sub(pattern_email, '[EMAIL]', newText)
+ new_text = re.sub(r'\b' + PATTERN_EMAIL, '[EMAIL]', new_text)
elif self.preserve_email_hostname and not self.partially_preserve_email_username:
- newText = re.sub(pattern_email, '[FILTERED]' + '@' + '\\1', newText)
+ new_text = re.sub(r'\b' + PATTERN_EMAIL, '[FILTERED]' + '@' + '\\1', new_text)
else:
- pattern_short_email = r'\b[\w][\w_\-\.\+]{0,5}@'
- pattern_short_email += '(' + pattern_hostname + ')'
- newText = re.sub(pattern_short_email, '[FILTERED]@' + '\\1', newText)
- pattern_long_mail = r'\b([\w][\w_\-\.\+]{2})[\w_\-\.\+]+@'
- pattern_long_mail += r'(' + pattern_hostname + ')'
- newText = re.sub(pattern_long_mail, '\\1' + '[FILTERED]@' + '\\2', newText)
-
+ new_text = re.sub(pattern_short_email, '[FILTERED]@' + '\\1', new_text)
+ new_text = re.sub(pattern_long_mail, '\\1' + '[FILTERED]@' + '\\2', new_text)
if not self.preserve_phone_country_code:
- newText = re.sub(phone, '[PHONE]', newText)
+ new_text = re.sub(PATTERN_PHONE, '[PHONE]', new_text)
else:
- international = r'((\+|00)[1-9]\d{0,2})[-\(\)\s]{0,2}[1-9]([-\(\)\s]{0,2}\d){5,10}\b'
- newText = re.sub(international, '\\1' + ' [FILTERED]', newText)
- country = r'\b0([-\(\)\s]{0,2}\d){6,11}\b'
- newText = re.sub(country, '[FILTERED]', newText)
- return newText
+ new_text = re.sub(international, '\\1' + ' [FILTERED]', new_text)
+ new_text = re.sub(country, '[FILTERED]', new_text)
+ return new_text
class Validations:
def is_email(value):
- pattern_dom = r'(\w(([\w-]{0,61}?)\w)?\.)'
- pattern_tld = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?'
- pattern_hostname = pattern_dom + r'+' + pattern_tld + r'$'
- pattern = r'^[\w][\w_\-\.\+]{0,200}@'
- pattern += pattern_hostname
- return(bool(re.match(pattern, value)))
+ return(bool(re.match(r'^' + PATTERN_EMAIL + r'$', value)))
def is_phone(value):
- pattern = r'^(((\+|00)[1-9]\d{0,2})|0)[-\(\)\s]{0,2}[1-9]'
- pattern += r'([-\(\)\s]{0,2}\d){5,10}$'
- return bool(re.match(pattern, value))
+ return bool(re.match(r'^' + PATTERN_PHONE + r'$', value))
def is_hostname(value):
- pattern_dom = r'(\w(([\w-]{0,61}?)\w)?\.)'
- pattern_tld = r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?'
- pattern = r'^' + pattern_dom + r'+' + pattern_tld + r'$'
- return bool(re.match(pattern, value))
+ return bool(re.match(r'^' + PATTERN_HOSTNAME + r'$', value))
def is_ip_address(value):
- matchNum = r'(1?[1-9]?\d|2[0-4]\d|25[0-5])'
- pattern = r'^' + (matchNum + '\.') * 3 + matchNum + '$'
- return bool(re.match(pattern, value))
+ pattern_8bit_num = r'(1?[1-9]?\d|2[0-4]\d|25[0-5])'
+ pattern_ip_address = r'^' + (pattern_8bit_num + '\.') * 3
+ pattern_ip_address += pattern_8bit_num + '$'
+ return bool(re.match(pattern_ip_address, value))
def is_number(value):
- pattern = r'^-?(0|[1-9]\d*)([\.,]\d+)?$'
- return bool(re.match(pattern, value))
+ pattern_number = r'^-?(0|[1-9]\d*)([\.,]\d+)?$'
+ return bool(re.match(pattern_number, value))
def is_integer(value):
- pattern = r'^-?(0|[1-9]\d*)$'
- return bool(re.match(pattern, value))
+ pattern_integer = r'^-?(0|[1-9]\d*)$'
+ return bool(re.match(pattern_integer, value))
def is_date(value):
- pattern = r'^\d{4}-(02-([01]\d|2[1-8])|(01|03|05|07|08|10|12)'
- pattern += r'-([012]\d|3[01])|(04|06|09|11)-([012]\d|30))$'
- return bool(re.match(pattern, value))
+ return bool(re.match(r'^' + PATTERN_DATE + r'$', value))
def is_time(value):
- hour_patt = r'(2[0-3]|[01]\d|)'
- min_patt = r'[0-5]\d'
- pattern = r'^' + hour_patt + ":" + min_patt + ":" + min_patt + '$'
- return bool(re.match(pattern, value))
+ return bool(re.match(r'^' + PATTERN_TIME + '$', value))
def is_datetime(value):
- date_pattern = r'^\d{4}-(02-([01]\d|2[1-8])|(01|03|05|07|08|10|12)'
- date_pattern += r'-([012]\d|3[01])|(04|06|09|11)-([012]\d|30))'
+ pattern_datetime = r'^' + PATTERN_DATE + r'\s' + PATTERN_TIME + '$'
- hour_patt = r'(2[0-3]|[01]\d|)'
+ return bool(re.match(pattern_datetime, value))
- min_patt = r'[0-5]\d'
- time_pattern = hour_patt + ":" + min_patt + ":" + min_patt + '$'
- return bool(re.match(date_pattern + r'\s' + time_pattern, value))