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

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

Към профила на Валентин Петров

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 17 успешни тест(а)
  • 22 неуспешни тест(а)

Код

import re
class PrivacyFilter:
def __init__(self, input=''):
self.text = input
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
phone = re.compile(('((?![a-zA-Z0-9+]0.*)(0|\+)(?!0)|'
'(00|\+)(?!0\d{0,2}))\d{1,3}'
'([ \-()]{0,2}?\d){6,11}(?![\-()])'))
mail = re.compile(('\w[A-Za-z0-9_+.-]{,200}'
'@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+'
'(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?'))
phone_filter = phone.sub(self.processing_phone, self.text)
result = mail.sub(self.processing_mail, phone_filter)
return result
def processing_phone(self, match):
value = ''.join(match.group())
inter = re.match(('(00|\+)(?!0\d{0,2})\d{1,3}'
'([ \-()]{0,2}?\d){6,11}(?![\-()])'), value)
if self.preserve_phone_country_code and bool(inter):
int_code_match = re.search(('(00|\+)(?!0\d{0,2})'
'(9[976]\d|8[987530]\d|6[987]\d|'
'5[90]\d|42\d|3[875]\d|2[98654321]\d|'
'9[8543210]|8[6421]|6[6543210]|'
'5[87654321]|4[987654310]|3[9643210]|'
'2[70]|7|1)'), value)
start, end = int_code_match.span()
return value[:end]+' [FILTERED]'
return '[PHONE]'
def processing_mail(self, match):
value = ''.join(match.group())
if self.preserve_email_hostname and \
not self.partially_preserve_email_username:
hostname_match = re.search(('@\w((?![A-Za-z-]*-\.)'
'[A-Za-z-]{1,62}\.?)+'
'(\.[A-Za-z]{2,3})'
'(\.[A-Za-z]{2})?'), value)
start, end = hostname_match.span()
return '[FILTERED]'+value[start:end]
if self.partially_preserve_email_username:
hostname_match = re.search(('@\w((?![A-Za-z-]*-\.)'
'[A-Za-z-]{1,62}\.?)+'
'(\.[A-Za-z]{2,3})'
'(\.[A-Za-z]{2})?'), value)
start, end = hostname_match.span()
return value[:3]+'[FILTERED]'+value[start:end]
return '[EMAIL]'
class Validations:
def is_email(value):
return bool(re.match(('^\w[A-Za-z0-9_+.-]{,200}'
'@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+'
'(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$'), value))
def is_hostname(value):
return bool(re.match(('^\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+'
'(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$'), value))
def is_phone(value):
local = re.match(('^(?![a-zA-Z0-9+]0.*)(0|\+)(?!0)\d{1,3}'
'([ \-()]{0,2}?\d){6,11}(?![ \-()])$'), value)
inter = re.match(('^(00|\+)(?!0\d{0,2})\d{1,3}'
'([ \-()]{0,2}?\d){6,11}(?![ \-()])$'), value)
return bool(local) or bool(inter)
def is_ip_address(value):
return bool(re.match(('^((25[0-5]|2[0-4][0-9]|'
'[01]?[0-9][0-9]?)\.){3}'
'(25[0-5]|2[0-4][0-9]|'
'[01]?[0-9][0-9]?)$'), value))
def is_number(value):
return bool(re.match('\-?\d+\.?\d+?', value))
def is_integer(value):
return bool(re.match('\-?\d+', value))
def is_date(value):
return bool(re.match('\d{4}\-'
'(1[0-2]|0?\d)\-'
'(3[01]|[12]\d|0?\d)', value))
def is_time(value):
return bool(re.match('(2[0-3]|[01]\d)(\:[0-5]?\d){2}', value))
def is_datetime(value):
return bool(re.match(('\d{4}\-(1[0-2]|0?\d)\-(3[01]|[12]\d|0?\d)'
'(\s|T)'
'(2[0-3]|[01]\d)(\:[0-5]?\d){2}'), value))

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

..FFF.FF..E....FFFFF..FFFFFF...FF.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-hrm6q6/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-hrm6q6/solution.py", line 18, in filtered
    phone_filter = phone.sub(self.processing_phone, self.text)
  File "/tmp/d20140513-11348-hrm6q6/solution.py", line 33, in processing_phone
    start, end = int_code_match.span()
AttributeError: 'NoneType' object has no attribute 'span'

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


======================================================================
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-hrm6q6/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-hrm6q6/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '0005551234569' != '0[PHONE]'
- 0005551234569
+ 0[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-hrm6q6/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' != 'me@[FILTERED]@example.com'
- [FILTERED]@example.com
+ me@[FILTERED]@example.com
? +++


======================================================================
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-hrm6q6/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[EMAIL]' != 'larodi@x.com'
- [EMAIL]
+ larodi@x.com


======================================================================
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-hrm6q6/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-hrm6q6/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_invalid_hours_minutes_or_seconds (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-hrm6q6/test.py", line 272, in test_does_not_allow_invalid_hours_minutes_or_seconds
    self.assertFalse(solution.Validations.is_time('12:01:99'))
AssertionError: True is not false

======================================================================
FAIL: test_does_not_allow_invalid_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-hrm6q6/test.py", line 255, in test_does_not_allow_invalid_months_or_days_in_dates
    self.assertFalse(solution.Validations.is_date('2012-06-32'))
AssertionError: True is not false

======================================================================
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-hrm6q6/test.py", line 249, in test_does_not_allow_zero_months_or_days_in_dates
    self.assertFalse(solution.Validations.is_date('1000-00-01'))
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-hrm6q6/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-hrm6q6/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_multiline_strings_in_integer_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-hrm6q6/test.py", line 233, in test_handles_multiline_strings_in_integer_validation_properly
    self.assertFalse(solution.Validations.is_number("42\n24"))
AssertionError: True is not false

======================================================================
FAIL: test_handles_multiline_strings_in_numbers_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-hrm6q6/test.py", line 215, in test_handles_multiline_strings_in_numbers_validation_properly
    self.assertFalse(solution.Validations.is_number("42\n24"))
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-hrm6q6/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-hrm6q6/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-hrm6q6/test.py", line 281, in test_validates_datetime_values
    self.assertFalse(solution.Validations.is_datetime('2012-00-19T23:59:00'))
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-hrm6q6/test.py", line 169, in test_validates_hostnames
    self.assertTrue(solution.Validations.is_hostname('1.2.3.4.xip.io'))
AssertionError: False is not true

======================================================================
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-hrm6q6/test.py", line 224, in test_validates_more_complex_integers
    self.assertFalse(solution.Validations.is_integer('-42 '))
AssertionError: True 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-hrm6q6/test.py", line 204, in test_validates_more_complex_numbers
    self.assertTrue(solution.Validations.is_number('0'))
AssertionError: False 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-hrm6q6/test.py", line 197, in test_validates_numbers
    self.assertTrue(solution.Validations.is_number('9'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 39 tests in 0.058s

FAILED (failures=21, errors=1)

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

Валентин обнови решението на 23.04.2014 12:47 (преди над 10 години)

+class Validations:
+ def is_email(value):
+ return bool(re.match('^\w[A-Za-z0-9_+.-]{,200}' + \
+ '@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)*' + \
+ '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$', value))
+
+ def is_hostname(value):
+ return bool(re.match('^\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)*' + \
+ '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$', value))
+
+ def is_phone(value):
+ local = re.match('^(^|(?![a-zA-Z0-9+]))(0|\+)(?!0)\d{1,3}' + \
+ '([ \-()]{0,2}?\d){6,11}(?![ \-()])$', value)
+ inter = re.match('^(00|\+)(?!0\d{0,2})\d{1,3}' + \
+ '([ \-()]{0,2}?\d){6,11}(?![ \-()])$', value)
+ return bool(local) or bool(inter)
+
+ def is_ip_address(value):
+ return bool(re.match('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}' + \
+ '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$', value))
+
+ def is_number(value):
+ return bool(re.match('\-?\d+\.?\d+?', value))
+
+ def is_integer(value):
+ return bool(re.match('\-?\d+', value))
+
+ def is_date(value):
+ return bool(re.match('\d{4}\-(1[0-2]|0?\d)\-(3[01]|[12]\d|0?\d)', value))
+
+ def is_time(value):
+ return bool(re.match('(2[0-3]|[01]\d)(\:[0-5]?\d){2}', value))
+
+ def is_datetime(value):
+ return bool(re.match('\d{4}\-(1[0-2]|0?\d)\-(3[01]|[12]\d|0?\d)' + \
+ '(\s|T)' + \
+ '(2[0-3]|[01]\d)(\:[0-5]?\d){2}', value))

Валентин обнови решението на 23.04.2014 15:56 (преди над 10 години)

+import re
+
+
+class PrivacyFilter:
+ def __init__(self, input=''):
+ self.text = input
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ phone = re.compile('((?![a-zA-Z0-9+]0.*)(0|\+)(?!0)|' + \
+ '(00|\+)(?!0\d{0,2}))\d{1,3}' + \
+ '([ \-()]{0,2}?\d){6,11}(?![\-()])')
+ mail = re.compile('\w[A-Za-z0-9_+.-]{,200}' + \
+ '@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
+ '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?')
+ phone_filter = phone.sub(self.processing_phone, self.text)
+ print('phone_filter: {}'.format(phone_filter))
+ result = mail.sub(self.processing_mail, phone_filter)
+ print('filtered: {}'.format(result))
+ return result
+
+ def processing_phone(self, match):
+ value = ''.join(match.group())
+ print('Value phone: {}'.format(value))
+ inter = re.match('(00|\+)(?!0\d{0,2})\d{1,3}' + \
+ '([ \-()]{0,2}?\d){6,11}(?![\-()])', value)
+ if self.preserve_phone_country_code and bool(inter):
+ int_code_match = re.search('(00|\+)(?!0\d{0,2})' + \
+ '(9[976]\d|8[987530]\d|6[987]\d|' + \
+ '5[90]\d|42\d|3[875]\d|2[98654321]\d|' + \
+ '9[8543210]|8[6421]|6[6543210]|' + \
+ '5[87654321]|4[987654310]|3[9643210]|' + \
+ '2[70]|7|1)', value)
+ start, end = int_code_match.span()
+ print('int code: {}'.format(value[:end]))
+ print('processing_phone: {}'.format(value[:end]+' [FILTERED]'))
+ return value[:end]+' [FILTERED]'
+ return '[PHONE]'
+
+ def processing_mail(self, match):
+ value = ''.join(match.group())
+ print('Value_email: {}'.format(value))
+ if self.preserve_email_hostname and not self.partially_preserve_email_username:
+ hostname_match = re.search('@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
+ '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?', value)
+ start, end = hostname_match.span()
+ return '[FILTERED]'+value[start:end]
+ if self.partially_preserve_email_username:
+ hostname_match = re.search('@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
+ '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?', value)
+ start, end = hostname_match.span()
+ return value[:3]+'[FILTERED]'+value[start:end]
+ return '[EMAIL]'
+
+
class Validations:
def is_email(value):
return bool(re.match('^\w[A-Za-z0-9_+.-]{,200}' + \
- '@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)*' + \
+ '@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
'(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$', value))
-
+
def is_hostname(value):
- return bool(re.match('^\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)*' + \
+ return bool(re.match('^\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
'(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$', value))
-
+
def is_phone(value):
- local = re.match('^(^|(?![a-zA-Z0-9+]))(0|\+)(?!0)\d{1,3}' + \
+ local = re.match('^(?![a-zA-Z0-9+]0.*)(0|\+)(?!0)\d{1,3}' + \
'([ \-()]{0,2}?\d){6,11}(?![ \-()])$', value)
inter = re.match('^(00|\+)(?!0\d{0,2})\d{1,3}' + \
'([ \-()]{0,2}?\d){6,11}(?![ \-()])$', value)
return bool(local) or bool(inter)
-
+
def is_ip_address(value):
return bool(re.match('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}' + \
'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$', value))
-
+
def is_number(value):
return bool(re.match('\-?\d+\.?\d+?', value))
-
+
def is_integer(value):
return bool(re.match('\-?\d+', value))
-
+
def is_date(value):
return bool(re.match('\d{4}\-(1[0-2]|0?\d)\-(3[01]|[12]\d|0?\d)', value))
-
+
def is_time(value):
return bool(re.match('(2[0-3]|[01]\d)(\:[0-5]?\d){2}', value))
-
+
def is_datetime(value):
return bool(re.match('\d{4}\-(1[0-2]|0?\d)\-(3[01]|[12]\d|0?\d)' + \
'(\s|T)' + \
'(2[0-3]|[01]\d)(\:[0-5]?\d){2}', value))

Валентин обнови решението на 23.04.2014 15:58 (преди над 10 години)

import re
class PrivacyFilter:
def __init__(self, input=''):
self.text = input
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
phone = re.compile('((?![a-zA-Z0-9+]0.*)(0|\+)(?!0)|' + \
'(00|\+)(?!0\d{0,2}))\d{1,3}' + \
'([ \-()]{0,2}?\d){6,11}(?![\-()])')
mail = re.compile('\w[A-Za-z0-9_+.-]{,200}' + \
'@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
'(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?')
phone_filter = phone.sub(self.processing_phone, self.text)
- print('phone_filter: {}'.format(phone_filter))
result = mail.sub(self.processing_mail, phone_filter)
- print('filtered: {}'.format(result))
return result
def processing_phone(self, match):
value = ''.join(match.group())
- print('Value phone: {}'.format(value))
inter = re.match('(00|\+)(?!0\d{0,2})\d{1,3}' + \
'([ \-()]{0,2}?\d){6,11}(?![\-()])', value)
if self.preserve_phone_country_code and bool(inter):
int_code_match = re.search('(00|\+)(?!0\d{0,2})' + \
'(9[976]\d|8[987530]\d|6[987]\d|' + \
'5[90]\d|42\d|3[875]\d|2[98654321]\d|' + \
'9[8543210]|8[6421]|6[6543210]|' + \
'5[87654321]|4[987654310]|3[9643210]|' + \
'2[70]|7|1)', value)
start, end = int_code_match.span()
- print('int code: {}'.format(value[:end]))
- print('processing_phone: {}'.format(value[:end]+' [FILTERED]'))
return value[:end]+' [FILTERED]'
return '[PHONE]'
def processing_mail(self, match):
value = ''.join(match.group())
- print('Value_email: {}'.format(value))
if self.preserve_email_hostname and not self.partially_preserve_email_username:
hostname_match = re.search('@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
'(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?', value)
start, end = hostname_match.span()
return '[FILTERED]'+value[start:end]
if self.partially_preserve_email_username:
hostname_match = re.search('@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
'(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?', value)
start, end = hostname_match.span()
return value[:3]+'[FILTERED]'+value[start:end]
return '[EMAIL]'
class Validations:
def is_email(value):
return bool(re.match('^\w[A-Za-z0-9_+.-]{,200}' + \
'@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
'(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$', value))
def is_hostname(value):
return bool(re.match('^\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
'(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$', value))
def is_phone(value):
local = re.match('^(?![a-zA-Z0-9+]0.*)(0|\+)(?!0)\d{1,3}' + \
'([ \-()]{0,2}?\d){6,11}(?![ \-()])$', value)
inter = re.match('^(00|\+)(?!0\d{0,2})\d{1,3}' + \
'([ \-()]{0,2}?\d){6,11}(?![ \-()])$', value)
return bool(local) or bool(inter)
def is_ip_address(value):
return bool(re.match('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}' + \
'(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$', value))
def is_number(value):
return bool(re.match('\-?\d+\.?\d+?', value))
def is_integer(value):
return bool(re.match('\-?\d+', value))
def is_date(value):
return bool(re.match('\d{4}\-(1[0-2]|0?\d)\-(3[01]|[12]\d|0?\d)', value))
def is_time(value):
return bool(re.match('(2[0-3]|[01]\d)(\:[0-5]?\d){2}', value))
def is_datetime(value):
return bool(re.match('\d{4}\-(1[0-2]|0?\d)\-(3[01]|[12]\d|0?\d)' + \
'(\s|T)' + \
'(2[0-3]|[01]\d)(\:[0-5]?\d){2}', value))

Валентин обнови решението на 23.04.2014 16:18 (преди над 10 години)

import re
class PrivacyFilter:
def __init__(self, input=''):
self.text = input
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
- phone = re.compile('((?![a-zA-Z0-9+]0.*)(0|\+)(?!0)|' + \
- '(00|\+)(?!0\d{0,2}))\d{1,3}' + \
- '([ \-()]{0,2}?\d){6,11}(?![\-()])')
- mail = re.compile('\w[A-Za-z0-9_+.-]{,200}' + \
- '@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
- '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?')
+ phone = re.compile(('((?![a-zA-Z0-9+]0.*)(0|\+)(?!0)|'
+ '(00|\+)(?!0\d{0,2}))\d{1,3}'
+ '([ \-()]{0,2}?\d){6,11}(?![\-()])'))
+ mail = re.compile(('\w[A-Za-z0-9_+.-]{,200}'
+ '@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+'
+ '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?'))
phone_filter = phone.sub(self.processing_phone, self.text)
result = mail.sub(self.processing_mail, phone_filter)
return result
def processing_phone(self, match):
value = ''.join(match.group())
- inter = re.match('(00|\+)(?!0\d{0,2})\d{1,3}' + \
- '([ \-()]{0,2}?\d){6,11}(?![\-()])', value)
+ inter = re.match(('(00|\+)(?!0\d{0,2})\d{1,3}'
+ '([ \-()]{0,2}?\d){6,11}(?![\-()])'), value)
if self.preserve_phone_country_code and bool(inter):
- int_code_match = re.search('(00|\+)(?!0\d{0,2})' + \
- '(9[976]\d|8[987530]\d|6[987]\d|' + \
- '5[90]\d|42\d|3[875]\d|2[98654321]\d|' + \
- '9[8543210]|8[6421]|6[6543210]|' + \
- '5[87654321]|4[987654310]|3[9643210]|' + \
- '2[70]|7|1)', value)
+ int_code_match = re.search(('(00|\+)(?!0\d{0,2})'
+ '(9[976]\d|8[987530]\d|6[987]\d|'
+ '5[90]\d|42\d|3[875]\d|2[98654321]\d|'
+ '9[8543210]|8[6421]|6[6543210]|'
+ '5[87654321]|4[987654310]|3[9643210]|'
+ '2[70]|7|1)'), value)
start, end = int_code_match.span()
return value[:end]+' [FILTERED]'
return '[PHONE]'
def processing_mail(self, match):
value = ''.join(match.group())
- if self.preserve_email_hostname and not self.partially_preserve_email_username:
- hostname_match = re.search('@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
- '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?', value)
+ if self.preserve_email_hostname and \
+ not self.partially_preserve_email_username:
+ hostname_match = re.search(('@\w((?![A-Za-z-]*-\.)'
+ '[A-Za-z-]{1,62}\.?)+'
+ '(\.[A-Za-z]{2,3})'
+ '(\.[A-Za-z]{2})?'), value)
start, end = hostname_match.span()
return '[FILTERED]'+value[start:end]
if self.partially_preserve_email_username:
- hostname_match = re.search('@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
- '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?', value)
+ hostname_match = re.search(('@\w((?![A-Za-z-]*-\.)'
+ '[A-Za-z-]{1,62}\.?)+'
+ '(\.[A-Za-z]{2,3})'
+ '(\.[A-Za-z]{2})?'), value)
start, end = hostname_match.span()
return value[:3]+'[FILTERED]'+value[start:end]
return '[EMAIL]'
class Validations:
def is_email(value):
- return bool(re.match('^\w[A-Za-z0-9_+.-]{,200}' + \
- '@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
- '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$', value))
+ return bool(re.match(('^\w[A-Za-z0-9_+.-]{,200}'
+ '@\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+'
+ '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$'), value))
def is_hostname(value):
- return bool(re.match('^\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+' + \
- '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$', value))
+ return bool(re.match(('^\w((?![A-Za-z-]*-\.)[A-Za-z-]{1,62}\.?)+'
+ '(\.[A-Za-z]{2,3})(\.[A-Za-z]{2})?$'), value))
def is_phone(value):
- local = re.match('^(?![a-zA-Z0-9+]0.*)(0|\+)(?!0)\d{1,3}' + \
- '([ \-()]{0,2}?\d){6,11}(?![ \-()])$', value)
- inter = re.match('^(00|\+)(?!0\d{0,2})\d{1,3}' + \
- '([ \-()]{0,2}?\d){6,11}(?![ \-()])$', value)
+ local = re.match(('^(?![a-zA-Z0-9+]0.*)(0|\+)(?!0)\d{1,3}'
+ '([ \-()]{0,2}?\d){6,11}(?![ \-()])$'), value)
+ inter = re.match(('^(00|\+)(?!0\d{0,2})\d{1,3}'
+ '([ \-()]{0,2}?\d){6,11}(?![ \-()])$'), value)
return bool(local) or bool(inter)
def is_ip_address(value):
- return bool(re.match('^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}' + \
- '(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$', value))
+ return bool(re.match(('^((25[0-5]|2[0-4][0-9]|'
+ '[01]?[0-9][0-9]?)\.){3}'
+ '(25[0-5]|2[0-4][0-9]|'
+ '[01]?[0-9][0-9]?)$'), value))
def is_number(value):
return bool(re.match('\-?\d+\.?\d+?', value))
def is_integer(value):
return bool(re.match('\-?\d+', value))
def is_date(value):
- return bool(re.match('\d{4}\-(1[0-2]|0?\d)\-(3[01]|[12]\d|0?\d)', value))
+ return bool(re.match('\d{4}\-'
+ '(1[0-2]|0?\d)\-'
+ '(3[01]|[12]\d|0?\d)', value))
def is_time(value):
return bool(re.match('(2[0-3]|[01]\d)(\:[0-5]?\d){2}', value))
def is_datetime(value):
- return bool(re.match('\d{4}\-(1[0-2]|0?\d)\-(3[01]|[12]\d|0?\d)' + \
- '(\s|T)' + \
+ return bool(re.match(('\d{4}\-(1[0-2]|0?\d)\-(3[01]|[12]\d|0?\d)'
- '(2[0-3]|[01]\d)(\:[0-5]?\d){2}', value))
+ '(\s|T)'
+ '(2[0-3]|[01]\d)(\:[0-5]?\d){2}'), value))