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

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

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

Резултати

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

Код

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 filter_email(self, txt):
if self.partially_preserve_email_username == True:
self.preserve_email_hostname = True
founded = re.findall(r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@', txt)
for f in founded:
f = re.split(r'.{3}', f, 1)
txt = re.sub(f[1], '[FILTERED]@', txt)
self.text = txt
elif self.preserve_email_hostname == True:
self.text = re.sub(
r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,199}@', '[FILTERED]@', txt)
else:
self.text = re.sub(
r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,199}@'
+
r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+[a-zA-Z]{2,3}'
+ r'(\.[a-zA-Z]{2,3})?', '[EMAIL]', txt)
def filter_phone(self, txt):
if self.preserve_phone_country_code == True:
txt = re.sub(
r'[^a-zA-Z0-9\+]0( |-|\(|\)){,2}[1-9]'
+ r'(( |-|\(|\)){,2}\d){5,10}', ' [PHONE]', txt)
founded = re.findall(
r'((\b00|\+)[1-9]\d{,2}(( |-|\(|\)){,2}\d){6,11})', txt)
for f in founded:
f = re.split(r'((\b00|\+)[1-9]\d{,2})', f[0], 1)
txt = re.sub(f[len(f) - 1], ' [FILTERED]', txt)
self.text = txt
else:
self.text = re.sub(
r'([^a-zA-Z0-9\+]0|\b00|\+)[1-9]\d{,2}'
+ r'(( |-|\(|\)){,2}\d){6,11}', ' [PHONE]', txt)
def filtered(self):
self.filter_email(self.text)
self.filter_phone(self.text)
return self.text
class Validations:
@classmethod
def is_email(cls, value):
matched = re.match(
r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,199}@'
+ r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+[a-zA-Z]{2,3}'
+ r'(\.[a-zA-Z]{2,3})?$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
@classmethod
def is_phone(cls, value):
matched = re.match(
r'([^a-zA-Z0-9\+]0|\b00|\+)[1-9]\d{,2}'
+ r'(( |-|\(|\)){,2}\d){6,11}$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
@classmethod
def is_hostname(cls, value):
matched = re.match(
r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+'
+ r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
@classmethod
def is_ip_address(cls, value):
matched = re.match(
r'((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}'
+ r'(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
@classmethod
def is_number(cls, value):
matched = re.match(r'-?(0|[1-9]\d*)(\.\d{1,})?$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
@classmethod
def is_integer(cls, value):
matched = re.match(r'-?(0|[1-9]\d*)$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
@classmethod
def is_date(cls, value):
matched = re.match(
r'\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
@classmethod
def is_time(cls, value):
matched = re.match(r'([01]\d|2[0-3]):[0-5]\d:[0-5]\d$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
@classmethod
def is_datetime(cls, value):
matched = re.match(
r'\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])( |T)'
+ r'([01]\d|2[0-3]):[0-5]\d:[0-5]\d$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True

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

..FFFFF..FF.....F...............F......
======================================================================
FAIL: test_does_not_brake_with_unicode (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-9io85b/test.py", line 64, in test_does_not_brake_with_unicode
    self.assertEqual('За връзка: [FILTERED]@example.com', self.partially_filter_email_usernames('За връзка: me@example.com'))
AssertionError: 'За връзка: [FILTERED]@example.com' != '[FILTERED]@З[FILTERED]@а[FILTERED]@ [FILTERED]@в[FILTERED]@р[FILTERED]@ъ[FILTER [truncated]...
- За връзка: [FILTERED]@example.com
+ [FILTERED]@З[FILTERED]@а[FILTERED]@ [FILTERED]@в[FILTERED]@р[FILTERED]@ъ[FILTERED]@з[FILTERED]@к[FILTERED]@а[FILTERED]@:[FILTERED]@ [FILTERED]@m[FILTERED]@e[FILTERED]@@[FILTERED]@e[FILTERED]@x[FILTERED]@a[FILTERED]@m[FILTERED]@p[FILTERED]@l[FILTERED]@e[FILTERED]@.[FILTERED]@c[FILTERED]@o[FILTERED]@m[FILTERED]@


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


======================================================================
FAIL: test_filters_whole_email_usernames_if_too_short (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-9io85b/test.py", line 61, in test_filters_whole_email_usernames_if_too_short
    self.assertEqual('[FILTERED]@example.com', self.partially_filter_email_usernames('me@example.com'))
AssertionError: '[FILTERED]@example.com' != '[FILTERED]@m[FILTERED]@e[FILTERED]@@[FILTERED]@e[FILTERED]@x[FILTERED]@a[FILTER [truncated]...
- [FILTERED]@example.com
+ [FILTERED]@m[FILTERED]@e[FILTERED]@@[FILTERED]@e[FILTERED]@x[FILTERED]@a[FILTERED]@m[FILTERED]@p[FILTERED]@l[FILTERED]@e[FILTERED]@.[FILTERED]@c[FILTERED]@o[FILTERED]@m[FILTERED]@


======================================================================
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-9io85b/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] or...'
-  [PHONE] or...
+   [PHONE] 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-9io85b/test.py", line 100, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    self.assertEqual(filtered, filter.filtered())
AssertionError: 'Phone: +25 [FILTERED]' != 'Phone: +25( 55 )12 12255'
- Phone: +25 [FILTERED]
+ Phone: +25( 55 )12 12255


======================================================================
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-9io85b/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_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-9io85b/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.051s

FAILED (failures=9)

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

Константин обнови решението на 23.04.2014 13:43 (преди над 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 filter_email(self, txt):
+ if self.partially_preserve_email_username == True:
+ self.preserve_email_hostname = True
+ founded = re.findall(r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@', txt)
+ for f in founded:
+ f = re.split(r'.{3}', f, 1)
+ txt = re.sub(f[1], '[FILTERED]@', txt)
+
+ self.text = txt
+ elif self.preserve_email_hostname == True:
+ self.text = re.sub(
+ r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@', '[FILTERED]@', txt)
+ else:
+ self.text = re.sub(
+ r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@'
+ +
+ r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+[a-zA-Z]{2,3}'
+ + r'(\.[a-zA-Z]{2,3})?', '[EMAIL]', txt)
+
+ def filter_phone(self, txt):
+ if self.preserve_phone_country_code == True:
+ txt = re.sub(
+ r'[^a-zA-Z0-9\+]0( |-|\(|\)){,2}[1-9]'
+ + r'(( |-|\(|\)){,2}\d){5,10}', ' [PHONE]', txt)
+
+ founded = re.findall(
+ r'((\b00|\+)[1-9]\d{,2}(( |-|\(|\)){,2}\d){6,11})', txt)
+ for f in founded:
+ f = re.split(r'((\b00|\+)[1-9]\d{,2})', f[0], 1)
+ txt = re.sub(f[len(f) - 1], ' [FILTERED]', txt)
+
+ self.text = txt
+ else:
+ self.text = re.sub(
+ r'([^a-zA-Z0-9\+]0|\b00|\+)[1-9]\d{,2}'
+ + r'(( |-|\(|\)){,2}\d){6,11}', ' [PHONE]', txt)
+
+ def filtered(self):
+ self.filter_email(self.text)
+ self.filter_phone(self.text)
+ return self.text
+
+
+class Validations:
+
+ def is_email(value):
+ matched = re.match(
+ r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@'
+ + r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+[a-zA-Z]{2,3}'
+ + r'(\.[a-zA-Z]{2,3})?$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_phone(value):
+ matched = re.match(
+ r'([^a-zA-Z0-9\+]0|\b00|\+)[1-9]\d{,2}'
+ + r'(( |-|\(|\)){,2}\d){6,11}$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_hostname(value):
+ matched = re.match(
+ r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+'
+ + r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_ip_address(value):
+ matched = re.match(
+ r'((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}'
+ + r'(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_number(value):
+ matched = re.match(r'-?(0|[1-9]\d*)(\.\d{1,})?$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_integer(value):
+ matched = re.match(r'-?(0|[1-9]\d*)$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_date(value):
+ matched = re.match(
+ r'\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_time(value):
+ matched = re.match(r'([01]\d|2[0-3]):[0-5]\d:[0-5]\d$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True
+
+ def is_datetime(value):
+ matched = re.match(
+ r'\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])( |T)'
+ + r'([01]\d|2[0-3]):[0-5]\d:[0-5]\d$', value)
+ if matched == None:
+ return False
+ elif matched.group(0) == value:
+ return True

Константин обнови решението на 23.04.2014 15:48 (преди над 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 filter_email(self, txt):
if self.partially_preserve_email_username == True:
self.preserve_email_hostname = True
founded = re.findall(r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@', txt)
for f in founded:
f = re.split(r'.{3}', f, 1)
txt = re.sub(f[1], '[FILTERED]@', txt)
self.text = txt
elif self.preserve_email_hostname == True:
self.text = re.sub(
- r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@', '[FILTERED]@', txt)
+ r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,199}@', '[FILTERED]@', txt)
else:
self.text = re.sub(
- r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@'
+ r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,199}@'
+
r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+[a-zA-Z]{2,3}'
+ r'(\.[a-zA-Z]{2,3})?', '[EMAIL]', txt)
def filter_phone(self, txt):
if self.preserve_phone_country_code == True:
txt = re.sub(
r'[^a-zA-Z0-9\+]0( |-|\(|\)){,2}[1-9]'
+ r'(( |-|\(|\)){,2}\d){5,10}', ' [PHONE]', txt)
founded = re.findall(
r'((\b00|\+)[1-9]\d{,2}(( |-|\(|\)){,2}\d){6,11})', txt)
for f in founded:
f = re.split(r'((\b00|\+)[1-9]\d{,2})', f[0], 1)
txt = re.sub(f[len(f) - 1], ' [FILTERED]', txt)
self.text = txt
else:
self.text = re.sub(
r'([^a-zA-Z0-9\+]0|\b00|\+)[1-9]\d{,2}'
+ r'(( |-|\(|\)){,2}\d){6,11}', ' [PHONE]', txt)
def filtered(self):
self.filter_email(self.text)
self.filter_phone(self.text)
return self.text
class Validations:
- def is_email(value):
+ @classmethod
+ def is_email(cls, value):
matched = re.match(
- r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,200}@'
+ r'[a-zA-Z0-9][a-zA-Z0-9-_+\.]{,199}@'
+ r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+[a-zA-Z]{2,3}'
+ r'(\.[a-zA-Z]{2,3})?$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
- def is_phone(value):
+ @classmethod
+ def is_phone(cls, value):
matched = re.match(
r'([^a-zA-Z0-9\+]0|\b00|\+)[1-9]\d{,2}'
+ r'(( |-|\(|\)){,2}\d){6,11}$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
- def is_hostname(value):
+ @classmethod
+ def is_hostname(cls, value):
matched = re.match(
r'([a-zA-Z0-9][a-zA-Z0-9-]{,61}([a-zA-Z0-9])?\.)+'
+ r'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
- def is_ip_address(value):
+ @classmethod
+ def is_ip_address(cls, value):
matched = re.match(
r'((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}'
+ r'(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
- def is_number(value):
+ @classmethod
+ def is_number(cls, value):
matched = re.match(r'-?(0|[1-9]\d*)(\.\d{1,})?$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
- def is_integer(value):
+ @classmethod
+ def is_integer(cls, value):
matched = re.match(r'-?(0|[1-9]\d*)$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
- def is_date(value):
+ @classmethod
+ def is_date(cls, value):
matched = re.match(
r'\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
- def is_time(value):
+ @classmethod
+ def is_time(cls, value):
matched = re.match(r'([01]\d|2[0-3]):[0-5]\d:[0-5]\d$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True
- def is_datetime(value):
+ @classmethod
+ def is_datetime(cls, value):
matched = re.match(
r'\d{4}-(0[1-9]|1[012])-(0[1-9]|[12]\d|3[01])( |T)'
+ r'([01]\d|2[0-3]):[0-5]\d:[0-5]\d$', value)
if matched == None:
return False
elif matched.group(0) == value:
return True