Решение на Регулярни изрази от Йончо Йончев

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

Към профила на Йончо Йончев

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 19 успешни тест(а)
  • 20 неуспешни тест(а)

Код

import re
class PrivacyFilter(object):
'''Obfuscating the text'''
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):
if self.preserve_phone_country_code:
self.text=re.sub("^(([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[FILTERED]",self.text)
else:
self.text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[PHONE]",self.text)
if self.preserve_email_hostname:
self.text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199})$","[FILTERED]",self.text)
else:
self.text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[EMAIL]",self.text)
if self.partially_preserve_email_username:
self.text=re.sub("^(@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[FILTERED]",self.text)
else:
self.text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[EMAIL]",self.text)
return self.text
class Validations:
'''validators'''
def is_email(value):
return re.match("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$", value )
def is_phone(value):
return re.match("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$",value)
def is_hostname(value):
return re.match("^([a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$",value)
def is_ip_address(value):
return re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", value )
def is_number(value):
return re.match("^(-?[0-9]+.?[0-9]+)$",value )
def is_integer(value):
return re.match("^(-?[0-9]+)$",value)
def is_date(value):
return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1]))$",value)
def is_time(value):
return re.match("^(([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )
def is_datetime(value):
return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1])[' ''T']([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )

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

FFF..FFFFFE....FF.....FF..FFF...F.FFF..
======================================================================
ERROR: 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-1jbrgxf/test.py", line 100, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    self.assertEqual(filtered, filter.filtered())
  File "/tmp/d20140513-11348-1jbrgxf/solution.py", line 15, in filtered
    self.text=re.sub("^(([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[FILTERED]",self.text)
  File "/opt/python3.3/lib/python3.3/re.py", line 170, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/opt/python3.3/lib/python3.3/functools.py", line 258, in wrapper
    result = user_function(*args, **kwds)
  File "/opt/python3.3/lib/python3.3/re.py", line 274, in _compile
    return sre_compile.compile(pattern, flags)
  File "/opt/python3.3/lib/python3.3/sre_compile.py", line 493, in compile
    p = sre_parse.parse(p, flags)
  File "/opt/python3.3/lib/python3.3/sre_parse.py", line 724, in parse
    p = _parse_sub(source, pattern, 0)
  File "/opt/python3.3/lib/python3.3/sre_parse.py", line 347, in _parse_sub
    itemsappend(_parse(source, state))
  File "/opt/python3.3/lib/python3.3/sre_parse.py", line 674, in _parse
    raise error("unbalanced parenthesis")
sre_constants.error: unbalanced parenthesis

======================================================================
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-1jbrgxf/test.py", line 54, in test_allows_email_hostname_to_be_preserved
    self.assertEqual('[FILTERED]@example.com', self.filter_email_usernames('someone@example.com'))
AssertionError: '[FILTERED]@example.com' != '[EMAIL]'
- [FILTERED]@example.com
+ [EMAIL]


======================================================================
FAIL: test_allows_email_usernames_to_be_partially_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-1jbrgxf/test.py", line 58, in test_allows_email_usernames_to_be_partially_preserved
    self.assertEqual('som[FILTERED]@example.com', self.partially_filter_email_usernames('someone@example.com'))
AssertionError: 'som[FILTERED]@example.com' != '[EMAIL]'
- som[FILTERED]@example.com
+ [EMAIL]


======================================================================
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-1jbrgxf/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' != 'За връзка: me@example.com'
- За връзка: [FILTERED]@example.com
?            ^^^^^^^^^^
+ За връзка: me@example.com
?            ^^


======================================================================
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-1jbrgxf/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Reach me at: [PHONE]' != 'Reach me at: 0885123123'
- Reach me at: [PHONE]
+ Reach me at: 0885123123


======================================================================
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-1jbrgxf/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' != '[EMAIL]'
- [FILTERED]@example.com
+ [EMAIL]


======================================================================
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-1jbrgxf/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact: [EMAIL],[EMAIL]' != 'Contact: 1someone@example.com,someone.new@sub.example123.co.uk'
- Contact: [EMAIL],[EMAIL]
+ Contact: 1someone@example.com,someone.new@sub.example123.co.uk


======================================================================
FAIL: test_obfuscates_simple_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-1jbrgxf/test.py", line 24, in test_obfuscates_simple_emails
    self.assertEqual('Contact: [EMAIL]', solution.PrivacyFilter('Contact: someone@example.com').filtered())
AssertionError: 'Contact: [EMAIL]' != 'Contact: someone@example.com'
- Contact: [EMAIL]
+ Contact: someone@example.com


======================================================================
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-1jbrgxf/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...' != ' +359881212-12-1 2 or...'
-  [PHONE] or...
+  +359881212-12-1 2 or...


======================================================================
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-1jbrgxf/test.py", line 124, in test_can_validate_more_complex_emails
    self.assertIs(solution.Validations.is_email(email), valid)
AssertionError: <_sre.SRE_Match object at 0xb77ea4e8> is not True

======================================================================
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-1jbrgxf/test.py", line 160, in test_can_validate_more_complex_phone_numbers
    self.assertIs(solution.Validations.is_phone(phone), valid)
AssertionError: None 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-1jbrgxf/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: <_sre.SRE_Match object at 0xb78177a0> 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-1jbrgxf/test.py", line 179, in test_handles_multiline_strings_in_hostname_validation_properly
    self.assertFalse(solution.Validations.is_hostname("foo.com\n"))
AssertionError: <_sre.SRE_Match object at 0xb7803ba8> 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-1jbrgxf/test.py", line 259, in test_handles_newlines_in_date_validation
    self.assertFalse(solution.Validations.is_date("2012-11-19\n"))
AssertionError: <_sre.SRE_Match object at 0xb7802288> 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-1jbrgxf/test.py", line 288, in test_handles_newlines_in_time_and_datetime_validation
    self.assertFalse(solution.Validations.is_time("12:01:01\n"))
AssertionError: <_sre.SRE_Match object at 0xb78158b8> is not false

======================================================================
FAIL: test_returns_boolean_True_or_False (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-1jbrgxf/test.py", line 109, in test_returns_boolean_True_or_False
    self.assertIs(solution.Validations.is_email('foo@bar.com'), True)
AssertionError: <_sre.SRE_Match object at 0xb6f9ca40> 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-1jbrgxf/test.py", line 174, in test_validates_hostnames
    self.assertFalse(solution.Validations.is_hostname('not-a-hostname-.com'))
AssertionError: <_sre.SRE_Match object at 0xb6f9c650> is not false

======================================================================
FAIL: test_validates_more_complex_integers (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-1jbrgxf/test.py", line 225, in test_validates_more_complex_integers
    self.assertFalse(solution.Validations.is_integer('00'))
AssertionError: <_sre.SRE_Match object at 0xb77dfb60> is not false

======================================================================
FAIL: test_validates_more_complex_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-1jbrgxf/test.py", line 204, in test_validates_more_complex_numbers
    self.assertTrue(solution.Validations.is_number('0'))
AssertionError: None is not true

======================================================================
FAIL: test_validates_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-1jbrgxf/test.py", line 197, in test_validates_numbers
    self.assertTrue(solution.Validations.is_number('9'))
AssertionError: None is not true

----------------------------------------------------------------------
Ran 39 tests in 0.052s

FAILED (failures=19, errors=1)

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

Йончо обнови решението на 23.04.2014 15:38 (преди около 10 години)

+import re
+
+class PrivacyFilter:
+ preserve_phone_country_code=False
+ preserve_email_hostname=False
+ partially_preserve_email_username=False
+
+ def filtered(text):
+ return text
+
+
+
+class Validations:
+
+ def is_email(value):
+ return re.match("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$", value )
+
+ def is_phone(value):
+ return re.match("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$ ",value)
+
+ def is_hostname(value):
+ return re.match("^([a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$",value)
+
+ def is_ip_address(value):
+ return re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", value )
+ def is_number(value):
+ return re.match("^(-?[0-9]+.?[0-9]+)$",value )
+
+ def is_integer(value):
+ return re.match("^(-?[0-9]+)$",value)
+
+ def is_date(value):
+ return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1]))$",value)
+
+ def is_time(value):
+ return re.match("^(([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )
+
+ def is_datetime(value):
+ return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1])[' ''T']([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )

Йончо обнови решението на 23.04.2014 16:03 (преди около 10 години)

import re
-class PrivacyFilter:
+class PrivacyFilter(object):
preserve_phone_country_code=False
preserve_email_hostname=False
partially_preserve_email_username=False
def filtered(text):
+ if preserve_phone_country_code:
+ text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[FILTERED]",text)
+ else:
+ text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[PHONE]",text)
+ if preserve_email_hostname:
+ text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[FILTERED]",text)
+ else:
+ text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[EMAIL]",text)
+ if preserve_email_hostname:
+ text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[FILTERED]",text)
+ else:
+ text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[EMAIL]",text)
return text
class Validations:
def is_email(value):
return re.match("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$", value )
def is_phone(value):
- return re.match("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$ ",value)
+ return re.match("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$",value)
def is_hostname(value):
return re.match("^([a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$",value)
def is_ip_address(value):
return re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", value )
def is_number(value):
return re.match("^(-?[0-9]+.?[0-9]+)$",value )
def is_integer(value):
return re.match("^(-?[0-9]+)$",value)
def is_date(value):
return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1]))$",value)
def is_time(value):
return re.match("^(([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )
def is_datetime(value):
- return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1])[' ''T']([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )
+ return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1])[' ''T']([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )

Йончо обнови решението на 23.04.2014 16:27 (преди около 10 години)

import re
class PrivacyFilter(object):
- preserve_phone_country_code=False
- preserve_email_hostname=False
- partially_preserve_email_username=False
+
+
+ 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(text):
if preserve_phone_country_code:
- text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[FILTERED]",text)
+ text=re.sub("^(([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[FILTERED]",text)
else:
- text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[PHONE]",text)
+ text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[PHONE]",text)
if preserve_email_hostname:
- text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[FILTERED]",text)
+ text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199})$","[FILTERED]",text)
else:
- text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[EMAIL]",text)
- if preserve_email_hostname:
- text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[FILTERED]",text)
+ text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[EMAIL]",text)
+ if partially_preserve_email_username:
+ text=re.sub("^(@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[FILTERED]",text)
else:
- text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[EMAIL]",text)
+ text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[EMAIL]",text)
return text
class Validations:
def is_email(value):
return re.match("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$", value )
def is_phone(value):
return re.match("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$",value)
def is_hostname(value):
return re.match("^([a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$",value)
def is_ip_address(value):
return re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", value )
def is_number(value):
return re.match("^(-?[0-9]+.?[0-9]+)$",value )
def is_integer(value):
return re.match("^(-?[0-9]+)$",value)
def is_date(value):
return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1]))$",value)
def is_time(value):
return re.match("^(([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )
def is_datetime(value):
- return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1])[' ''T']([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )
+ return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1])[' ''T']([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )

Йончо обнови решението на 23.04.2014 16:43 (преди около 10 години)

import re
class PrivacyFilter(object):
+ '''Obfuscating the text'''
-
-
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(text):
- if preserve_phone_country_code:
- text=re.sub("^(([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[FILTERED]",text)
+ def filtered(self):
+ if self.preserve_phone_country_code:
+ self.text=re.sub("^(([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[FILTERED]",self.text)
else:
- text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[PHONE]",text)
- if preserve_email_hostname:
- text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199})$","[FILTERED]",text)
+ self.text=re.sub("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$","[PHONE]",self.text)
+ if self.preserve_email_hostname:
+ self.text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199})$","[FILTERED]",self.text)
else:
- text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[EMAIL]",text)
- if partially_preserve_email_username:
- text=re.sub("^(@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[FILTERED]",text)
+ self.text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[EMAIL]",self.text)
+ if self.partially_preserve_email_username:
+ self.text=re.sub("^(@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[FILTERED]",self.text)
else:
- text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[EMAIL]",text)
- return text
+ self.text=re.sub("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$","[EMAIL]",self.text)
+ return self.text
class Validations:
+ '''validators'''
def is_email(value):
return re.match("^([a-zA-Z0-9]{1}[a-zA-Z0-9._+-]{0,199}@[a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$", value )
def is_phone(value):
return re.match("^(([0]{2}|[+]{1})([1-9]{3})|0)([ ()-]{0,2}?[1-9][ ()-]{0,2}?){1}([0-9][ ()-]{0,2}?){5,10}[0-9]{1}$",value)
def is_hostname(value):
return re.match("^([a-zA-Z0-9]{1}[a-zA-Z0-9.-]{0,61}\.[a-zA-Z0-9]{2,3}(\.+[a-zA-Z0-9]{2,3})?)$",value)
def is_ip_address(value):
return re.match("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", value )
def is_number(value):
return re.match("^(-?[0-9]+.?[0-9]+)$",value )
def is_integer(value):
return re.match("^(-?[0-9]+)$",value)
def is_date(value):
return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1]))$",value)
def is_time(value):
return re.match("^(([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )
def is_datetime(value):
return re.match("^(([0-9][0-9][0-9][0-9])-([0][1-9]|1[0-2])-([0][1-9]|[12]\d|3[0-1])[' ''T']([01]\d|2[0-3]):([0-5]\d):([0-5]\d))$",value )