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

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

Към профила на Драгомир Тунчев

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 23 успешни тест(а)
  • 16 неуспешни тест(а)

Код

import re
HOSTNAME = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
EMAIL_ADDRESS_SYMBOLS = '(\w|[\.\-\+])'
EMAIL_ADDRESS = '[a-zA-Z0-9]{}{}@{}'\
.format(EMAIL_ADDRESS_SYMBOLS, '{,200}', HOSTNAME)
PHONE_CODE = '(0|((00|\+)[1-9](\d{2}|\d)?))'
PHONE_SEPARATORS = '[\s\-\(\)]{,2}'
PHONE_NUMBER = '({}\d){}({})?\d'\
.format(PHONE_SEPARATORS, '{5,10}', PHONE_SEPARATORS)
PHONE_FULL = '{}{}'.format(PHONE_CODE, PHONE_NUMBER)
IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4]\d|25[0-5])'
INTEGER = '-?(0|([1-9]\d*))'
DATE = '\d{4}\-([1-9]|1[0-2])\-([1-9]|[1-2]\d|3[0-1])'
TIME = '(0\d|1[\d|2[0-3]):[0-5]\d:[0-5]\d'
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 _preserve_email_hostname(self):
return re.sub(r'{}{}{}(?=@{})'.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS,
'{,200}', HOSTNAME),
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
result = re.sub(r'(?<=[^\w\.\-\+]){}{}{}(?=@{})'
.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS, '{,4}',
HOSTNAME),
'[FILTERED]', self.text)
return re.sub(r'(?<={}{}{}){}{}(?=@{})'
.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS, '{2}',
EMAIL_ADDRESS_SYMBOLS, '{,197}',
HOSTNAME),
'[FILTERED]', result)
def _filter_entire_email(self):
return re.sub(r'{}'.format(EMAIL_ADDRESS), '[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
for times in range(2, -1, -1):
result = re.sub('{}{}{}{}{}{}'
.format(r'((?<=00[1-9]\d{', times,
'})|(?<=\+[1-9]\d{', times,
'}))', PHONE_NUMBER),
' [FILTERED]', result)
result = self._filter_phone_number(result, '[FILTERED]')
return result
def _filter_phone_number(self, result, filtered='[PHONE]'):
return re.sub(r'{}'.format(PHONE_FULL), filtered, result)
def _filter_email(self):
if self.preserve_email_hostname:
return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
return self._partially_preserve_email_username()
else:
return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
return self._preserve_phone_country_code(result)
else:
return self._filter_phone_number(result)
def filtered(self):
return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
return re.match(r'^{}$'.format(EMAIL_ADDRESS), value)
@classmethod
def is_phone(self, value):
return re.match(r'^{}$'.format(PHONE_FULL), value)
@classmethod
def is_hostname(self, value):
return re.match(r'^{}$'.format(HOSTNAME), value)
@classmethod
def is_ip_address(self, value):
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'.format(ip=IP_RANGE), value)
@classmethod
def is_number(self, value):
return re.match(r'^{}\.?\d*$'.format(INTEGER), value)
@classmethod
def is_integer(self, value):
return re.match(r'^{}$'.format(INTEGER), value)
@classmethod
def is_date(self, value):
return re.match(r'^{}$'.format(DATE), value)
@classmethod
def is_time(self, value):
return re.match(r'^{}$'.format(TIME), value)
@classmethod
def is_datetime(self, value):
return re.match(r'^{}(\s|T){}$'.format(DATE, TIME), value)

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

...FF.F...F.F.FFF.....FF..FFF..F...F..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-hni5g4/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-hni5g4/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_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-hni5g4/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@example.com'
- [FILTERED]@example.com
+ me@example.com


======================================================================
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-hni5g4/test.py", line 100, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    self.assertEqual(filtered, filter.filtered())
AssertionError: 'Phone: 0025 [FILTERED]' != 'Phone: [FILTERED]5'
- Phone: 0025 [FILTERED]
?       -----
+ Phone: [FILTERED]5
?                  +


======================================================================
FAIL: test_allows_huge_years_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-hni5g4/test.py", line 246, in test_allows_huge_years_in_date_validation
    self.assertTrue(solution.Validations.is_date('9999-01-01'))
AssertionError: None is not true

======================================================================
FAIL: test_allows_zero_years_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-hni5g4/test.py", line 243, in test_allows_zero_years_in_date_validation
    self.assertTrue(solution.Validations.is_date('0000-01-01'))
AssertionError: None is not true

======================================================================
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-hni5g4/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 0xb781d8b8> 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-hni5g4/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-hni5g4/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 0xb781d910> 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-hni5g4/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 0xb7832070> 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-hni5g4/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 0xb7837188> 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-hni5g4/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 0xb781f5a0> 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-hni5g4/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 0xb781d968> is not True

======================================================================
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-hni5g4/test.py", line 280, in test_validates_datetime_values
    self.assertTrue(solution.Validations.is_datetime('9999-11-19T23:59:00'))
AssertionError: None is not true

======================================================================
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-hni5g4/test.py", line 205, in test_validates_more_complex_numbers
    self.assertFalse(solution.Validations.is_number('00'))
AssertionError: <_sre.SRE_Match object at 0xb781cc80> is not false

======================================================================
FAIL: test_validates_times (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-hni5g4/test.py", line 266, in test_validates_times
    self.assertTrue(solution.Validations.is_time('23:59:59'))
AssertionError: None is not true

----------------------------------------------------------------------
Ran 39 tests in 0.069s

FAILED (failures=16)

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

Драгомир обнови решението на 16.04.2014 01:28 (преди около 10 години)

+import re
+
+
+class Validations():
+
+ @staticmethod
+ def is_email(value):
+ return re.match(r'^[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
+ '([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
+
+ @staticmethod
+ def is_phone(value):
+ return re.match(r'^(0[1-9][0-9]*|00[1-9][0-9]{,2}|\+[1-9][0-9]{,2})'
+ '([0-9][\s\-\(\)]{,2}){6,10}[0-9]$', value)
+
+ @staticmethod
+ def is_hostname(value):
+ return re.match(r'^([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
+
+ @staticmethod
+ def is_ip_address(value):
+ return re.match(r'^'
+ '([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])\.'
+ '([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])\.'
+ '([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])\.'
+ '([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])'
+ '$', value)
+
+ @staticmethod
+ def is_number(value):
+ return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
+
+ @staticmethod
+ def is_integer(value):
+ return re.match(r'^-?0?[1-9]\d*$', value)
+
+ @staticmethod
+ def is_date(value):
+ return re.match(r'^[0-9]{4}\-([1-9]|1[0-2])\-'
+ '([1-9]|[1-2][0-9]|3[0-1])$', value)
+
+ @staticmethod
+ def is_time(value):
+ return re.match(r'^(0[0-9]|1[0-9]|2[0-3]):'
+ '[0-5][0-9]:[0-5][0-9]$', value)
+
+ @staticmethod
+ def is_date_time(value):
+ return re.match(r'^[0-9]{4}\-([1-9]|1[0-2])\-'
+ '([1-9]|[1-2][0-9]|3[0-1])'
+ '(\s|T)'
+ '(0[0-9]|1[0-9]|2[0-3]):'
+ '[0-5][0-9]:[0-5][0-9]$', value)

Драгомир обнови решението на 16.04.2014 03:08 (преди около 10 години)

import re
+class PrivacyFilter():
+ def __init__(this, text):
+ this.text = text
+ this.preserve_phone_country_code = False
+ this.preserve_email_hostname = False
+ this.partially_preserve_email_username = False
+
+ def filtered(this):
+ if this.preserve_email_hostname:
+ result = re.sub(r'([a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200})'
+ '(?=@([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)'
+ '*[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)',
+ '[FILTERED]', this.text)
+ elif this.partially_preserve_email_username:
+ result = re.sub(r'(?<=[a-zA-Z0-9][a-zA-Z0-9\.\-\_\+]{2})'
+ '[a-zA-Z0-9\.\-\_\+]{,200}'
+ '(?=@([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)',
+ '[FILTERED]', this.text)
+ else:
+ result = re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
+ '([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?',
+ '[EMAIL]', this.text)
+ if this.preserve_phone_country_code:
+ pass
+ else:
+ result = re.sub('(0[1-9][0-9]*|00[1-9][0-9]{,2}|\+[1-9][0-9]{,2})'
+ '([\s\-\(\)]{,2}[0-9]){5,10}[0-9]',
+ '[PHONE]', result)
+ return result
+
+
class Validations():
@staticmethod
def is_email(value):
return re.match(r'^[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
'([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
@staticmethod
def is_phone(value):
return re.match(r'^(0[1-9][0-9]*|00[1-9][0-9]{,2}|\+[1-9][0-9]{,2})'
- '([0-9][\s\-\(\)]{,2}){6,10}[0-9]$', value)
+ '([\s\-\(\)]{,2}[0-9]){5,10}[0-9]$', value)
@staticmethod
def is_hostname(value):
return re.match(r'^([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
@staticmethod
def is_ip_address(value):
return re.match(r'^'
'([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])\.'
'([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])\.'
'([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])\.'
'([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])'
'$', value)
@staticmethod
def is_number(value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
@staticmethod
def is_integer(value):
return re.match(r'^-?0?[1-9]\d*$', value)
@staticmethod
def is_date(value):
return re.match(r'^[0-9]{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2][0-9]|3[0-1])$', value)
@staticmethod
def is_time(value):
return re.match(r'^(0[0-9]|1[0-9]|2[0-3]):'
'[0-5][0-9]:[0-5][0-9]$', value)
@staticmethod
def is_date_time(value):
return re.match(r'^[0-9]{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2][0-9]|3[0-1])'
'(\s|T)'
'(0[0-9]|1[0-9]|2[0-3]):'
'[0-5][0-9]:[0-5][0-9]$', value)

Драгомир обнови решението на 16.04.2014 03:28 (преди около 10 години)

import re
class PrivacyFilter():
def __init__(this, text):
this.text = text
this.preserve_phone_country_code = False
this.preserve_email_hostname = False
this.partially_preserve_email_username = False
def filtered(this):
if this.preserve_email_hostname:
result = re.sub(r'([a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200})'
'(?=@([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)'
'*[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)',
'[FILTERED]', this.text)
elif this.partially_preserve_email_username:
result = re.sub(r'(?<=[a-zA-Z0-9][a-zA-Z0-9\.\-\_\+]{2})'
'[a-zA-Z0-9\.\-\_\+]{,200}'
'(?=@([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)',
'[FILTERED]', this.text)
else:
result = re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
'([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?',
'[EMAIL]', this.text)
if this.preserve_phone_country_code:
pass
else:
result = re.sub('(0[1-9][0-9]*|00[1-9][0-9]{,2}|\+[1-9][0-9]{,2})'
'([\s\-\(\)]{,2}[0-9]){5,10}[0-9]',
'[PHONE]', result)
return result
class Validations():
@staticmethod
def is_email(value):
return re.match(r'^[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
'([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
@staticmethod
def is_phone(value):
return re.match(r'^(0[1-9][0-9]*|00[1-9][0-9]{,2}|\+[1-9][0-9]{,2})'
'([\s\-\(\)]{,2}[0-9]){5,10}[0-9]$', value)
@staticmethod
def is_hostname(value):
return re.match(r'^([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
@staticmethod
def is_ip_address(value):
- return re.match(r'^'
- '([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])\.'
- '([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])\.'
- '([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])\.'
- '([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])'
- '$', value)
+ ip_range = '([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])'
+ return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
+ .format(ip=ip_range), value)
@staticmethod
def is_number(value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
@staticmethod
def is_integer(value):
return re.match(r'^-?0?[1-9]\d*$', value)
@staticmethod
def is_date(value):
return re.match(r'^[0-9]{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2][0-9]|3[0-1])$', value)
@staticmethod
def is_time(value):
return re.match(r'^(0[0-9]|1[0-9]|2[0-3]):'
'[0-5][0-9]:[0-5][0-9]$', value)
@staticmethod
def is_date_time(value):
return re.match(r'^[0-9]{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2][0-9]|3[0-1])'
'(\s|T)'
'(0[0-9]|1[0-9]|2[0-3]):'
'[0-5][0-9]:[0-5][0-9]$', value)

Драгомир обнови решението на 16.04.2014 15:12 (преди около 10 години)

import re
class PrivacyFilter():
def __init__(this, text):
this.text = text
this.preserve_phone_country_code = False
this.preserve_email_hostname = False
this.partially_preserve_email_username = False
def filtered(this):
if this.preserve_email_hostname:
- result = re.sub(r'([a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200})'
+ result = re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
'(?=@([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)'
'*[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)',
'[FILTERED]', this.text)
elif this.partially_preserve_email_username:
- result = re.sub(r'(?<=[a-zA-Z0-9][a-zA-Z0-9\.\-\_\+]{2})'
+ result = re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
'[a-zA-Z0-9\.\-\_\+]{,200}'
'(?=@([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)',
'[FILTERED]', this.text)
else:
result = re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
'([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?',
'[EMAIL]', this.text)
if this.preserve_phone_country_code:
pass
else:
- result = re.sub('(0[1-9][0-9]*|00[1-9][0-9]{,2}|\+[1-9][0-9]{,2})'
- '([\s\-\(\)]{,2}[0-9]){5,10}[0-9]',
+ result = re.sub('(0[1-9]\d*|00[1-9]\d{,2}|\+[1-9]\d{,2})'
+ '([\s\-\(\)]{,2}\d){5,10}\d',
'[PHONE]', result)
return result
class Validations():
@staticmethod
def is_email(value):
- return re.match(r'^[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
+ return re.match(r'^[a-zA-z0-9](\w|[\.\-\+]){,200}@'
'([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
@staticmethod
def is_phone(value):
- return re.match(r'^(0[1-9][0-9]*|00[1-9][0-9]{,2}|\+[1-9][0-9]{,2})'
- '([\s\-\(\)]{,2}[0-9]){5,10}[0-9]$', value)
+ return re.match(r'^(0[1-9]\d*|00[1-9]\d{,2}|\+[1-9]\d{,2})'
+ '([\s\-\(\)]{,2}\d){5,10}\d$', value)
@staticmethod
def is_hostname(value):
return re.match(r'^([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
@staticmethod
def is_ip_address(value):
- ip_range = '([0-9]|[1-9][0-9]|1[1-9][1-9]|2[0-4][0-9]|25[0-5])'
+ ip_range = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
.format(ip=ip_range), value)
@staticmethod
def is_number(value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
@staticmethod
def is_integer(value):
return re.match(r'^-?0?[1-9]\d*$', value)
@staticmethod
def is_date(value):
- return re.match(r'^[0-9]{4}\-([1-9]|1[0-2])\-'
- '([1-9]|[1-2][0-9]|3[0-1])$', value)
+ return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
+ '([1-9]|[1-2]\d|3[0-1])$', value)
@staticmethod
def is_time(value):
- return re.match(r'^(0[0-9]|1[0-9]|2[0-3]):'
- '[0-5][0-9]:[0-5][0-9]$', value)
+ return re.match(r'^(0\d|1[\d|2[0-3]):'
+ '[0-5]\d:[0-5]\d$', value)
@staticmethod
- def is_date_time(value):
- return re.match(r'^[0-9]{4}\-([1-9]|1[0-2])\-'
- '([1-9]|[1-2][0-9]|3[0-1])'
+ def is_datetime(value):
+ return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
+ '([1-9]|[1-2]\d|3[0-1])'
'(\s|T)'
- '(0[0-9]|1[0-9]|2[0-3]):'
- '[0-5][0-9]:[0-5][0-9]$', value)
+ '(0\d|1\d|2[0-3]):'
+ '[0-5]\d:[0-5]\d$', value)

Драгомир обнови решението на 16.04.2014 15:20 (преди около 10 години)

import re
class PrivacyFilter():
def __init__(this, text):
this.text = text
this.preserve_phone_country_code = False
this.preserve_email_hostname = False
this.partially_preserve_email_username = False
def filtered(this):
if this.preserve_email_hostname:
result = re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
'(?=@([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)'
- '*[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)',
+ '*[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', this.text)
elif this.partially_preserve_email_username:
result = re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
'[a-zA-Z0-9\.\-\_\+]{,200}'
'(?=@([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)',
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', this.text)
else:
result = re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
'([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?',
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?',
'[EMAIL]', this.text)
if this.preserve_phone_country_code:
pass
else:
result = re.sub('(0[1-9]\d*|00[1-9]\d{,2}|\+[1-9]\d{,2})'
'([\s\-\(\)]{,2}\d){5,10}\d',
'[PHONE]', result)
return result
class Validations():
@staticmethod
def is_email(value):
return re.match(r'^[a-zA-z0-9](\w|[\.\-\+]){,200}@'
'([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_phone(value):
return re.match(r'^(0[1-9]\d*|00[1-9]\d{,2}|\+[1-9]\d{,2})'
'([\s\-\(\)]{,2}\d){5,10}\d$', value)
@staticmethod
def is_hostname(value):
return re.match(r'^([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$', value)
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_ip_address(value):
ip_range = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
.format(ip=ip_range), value)
@staticmethod
def is_number(value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
@staticmethod
def is_integer(value):
return re.match(r'^-?0?[1-9]\d*$', value)
@staticmethod
def is_date(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])$', value)
@staticmethod
def is_time(value):
return re.match(r'^(0\d|1[\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)
@staticmethod
def is_datetime(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])'
'(\s|T)'
'(0\d|1\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)

Драгомир обнови решението на 16.04.2014 16:47 (преди около 10 години)

import re
class PrivacyFilter():
def __init__(this, text):
this.text = text
this.preserve_phone_country_code = False
this.preserve_email_hostname = False
this.partially_preserve_email_username = False
def filtered(this):
if this.preserve_email_hostname:
result = re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
- '(?=@([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)'
- '*[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
+ '(?=@([a-zA-Z0-9]'
+ '([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', this.text)
elif this.partially_preserve_email_username:
result = re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
- '[a-zA-Z0-9\.\-\_\+]{,200}'
- '(?=@([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
+ '[a-zA-Z0-9\.\-\_\+]{,197}'
+ '(?=@([a-zA-Z0-9]'
+ '([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', this.text)
else:
result = re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
'([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?',
'[EMAIL]', this.text)
if this.preserve_phone_country_code:
- pass
+ result = re.sub(r'((?<=00[1-9])|(?<=00[1-9]\d)|'
+ '(?<=00[1-9]\d{2})|(?<=00[1-9]\d{3})|'
+ '(?<=\+[1-9])|(?<=\+[1-9]\d)|(?<=\+[1-9]\d{2})|'
+ '(?<=\+[1-9]\d{3}))'
+ '([\s\-\(\)]{,2}\d){5,10}\d',
+ ' [FILTERED]', result)
else:
- result = re.sub('(0[1-9]\d*|00[1-9]\d{,2}|\+[1-9]\d{,2})'
+ result = re.sub(r'(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d',
'[PHONE]', result)
return result
class Validations():
@staticmethod
def is_email(value):
return re.match(r'^[a-zA-z0-9](\w|[\.\-\+]){,200}@'
- '([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
+ '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_phone(value):
- return re.match(r'^(0[1-9]\d*|00[1-9]\d{,2}|\+[1-9]\d{,2})'
+ return re.match(r'^(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d$', value)
@staticmethod
def is_hostname(value):
- return re.match(r'^([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
+ return re.match(r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_ip_address(value):
ip_range = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
.format(ip=ip_range), value)
@staticmethod
def is_number(value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
@staticmethod
def is_integer(value):
return re.match(r'^-?0?[1-9]\d*$', value)
@staticmethod
def is_date(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])$', value)
@staticmethod
def is_time(value):
return re.match(r'^(0\d|1[\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)
@staticmethod
def is_datetime(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])'
'(\s|T)'
'(0\d|1\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)

Драгомир обнови решението на 16.04.2014 16:48 (преди около 10 години)

import re
class PrivacyFilter():
def __init__(this, text):
this.text = text
this.preserve_phone_country_code = False
this.preserve_email_hostname = False
this.partially_preserve_email_username = False
def filtered(this):
if this.preserve_email_hostname:
result = re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
'(?=@([a-zA-Z0-9]'
'([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', this.text)
elif this.partially_preserve_email_username:
result = re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
'[a-zA-Z0-9\.\-\_\+]{,197}'
'(?=@([a-zA-Z0-9]'
'([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', this.text)
else:
result = re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
- '([a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\.)*'
+ '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\)?.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?',
'[EMAIL]', this.text)
if this.preserve_phone_country_code:
result = re.sub(r'((?<=00[1-9])|(?<=00[1-9]\d)|'
'(?<=00[1-9]\d{2})|(?<=00[1-9]\d{3})|'
'(?<=\+[1-9])|(?<=\+[1-9]\d)|(?<=\+[1-9]\d{2})|'
'(?<=\+[1-9]\d{3}))'
'([\s\-\(\)]{,2}\d){5,10}\d',
' [FILTERED]', result)
else:
result = re.sub(r'(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d',
'[PHONE]', result)
return result
class Validations():
@staticmethod
def is_email(value):
return re.match(r'^[a-zA-z0-9](\w|[\.\-\+]){,200}@'
'([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_phone(value):
return re.match(r'^(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d$', value)
@staticmethod
def is_hostname(value):
return re.match(r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_ip_address(value):
ip_range = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
.format(ip=ip_range), value)
@staticmethod
def is_number(value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
@staticmethod
def is_integer(value):
return re.match(r'^-?0?[1-9]\d*$', value)
@staticmethod
def is_date(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])$', value)
@staticmethod
def is_time(value):
return re.match(r'^(0\d|1[\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)
@staticmethod
def is_datetime(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])'
'(\s|T)'
'(0\d|1\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)

Драгомир обнови решението на 16.04.2014 16:54 (преди около 10 години)

import re
class PrivacyFilter():
def __init__(this, text):
this.text = text
this.preserve_phone_country_code = False
this.preserve_email_hostname = False
this.partially_preserve_email_username = False
def filtered(this):
if this.preserve_email_hostname:
result = re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
'(?=@([a-zA-Z0-9]'
'([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', this.text)
elif this.partially_preserve_email_username:
result = re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
'[a-zA-Z0-9\.\-\_\+]{,197}'
'(?=@([a-zA-Z0-9]'
'([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', this.text)
else:
result = re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
- '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9]\)?.)*'
+ '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?',
'[EMAIL]', this.text)
if this.preserve_phone_country_code:
result = re.sub(r'((?<=00[1-9])|(?<=00[1-9]\d)|'
'(?<=00[1-9]\d{2})|(?<=00[1-9]\d{3})|'
'(?<=\+[1-9])|(?<=\+[1-9]\d)|(?<=\+[1-9]\d{2})|'
'(?<=\+[1-9]\d{3}))'
'([\s\-\(\)]{,2}\d){5,10}\d',
' [FILTERED]', result)
else:
result = re.sub(r'(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d',
'[PHONE]', result)
return result
class Validations():
@staticmethod
def is_email(value):
return re.match(r'^[a-zA-z0-9](\w|[\.\-\+]){,200}@'
'([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_phone(value):
return re.match(r'^(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d$', value)
@staticmethod
def is_hostname(value):
return re.match(r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_ip_address(value):
ip_range = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
.format(ip=ip_range), value)
@staticmethod
def is_number(value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
@staticmethod
def is_integer(value):
return re.match(r'^-?0?[1-9]\d*$', value)
@staticmethod
def is_date(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])$', value)
@staticmethod
def is_time(value):
return re.match(r'^(0\d|1[\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)
@staticmethod
def is_datetime(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])'
'(\s|T)'
'(0\d|1\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)

Драгомир обнови решението на 16.04.2014 17:17 (преди около 10 години)

import re
class PrivacyFilter():
def __init__(this, text):
this.text = text
this.preserve_phone_country_code = False
this.preserve_email_hostname = False
this.partially_preserve_email_username = False
def filtered(this):
if this.preserve_email_hostname:
result = re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
'(?=@([a-zA-Z0-9]'
'([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', this.text)
elif this.partially_preserve_email_username:
result = re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
'[a-zA-Z0-9\.\-\_\+]{,197}'
'(?=@([a-zA-Z0-9]'
'([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', this.text)
else:
result = re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
'([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?',
'[EMAIL]', this.text)
if this.preserve_phone_country_code:
- result = re.sub(r'((?<=00[1-9])|(?<=00[1-9]\d)|'
- '(?<=00[1-9]\d{2})|(?<=00[1-9]\d{3})|'
- '(?<=\+[1-9])|(?<=\+[1-9]\d)|(?<=\+[1-9]\d{2})|'
- '(?<=\+[1-9]\d{3}))'
- '([\s\-\(\)]{,2}\d){5,10}\d',
- ' [FILTERED]', result)
+ for times in range(2, -1, -1):
+ result = re.sub('{}{}{}{}{}'
+ .format(r'((?<=00[1-9]\d{', times,
+ '})|(?<=\+[1-9]\d{', times,
+ '}))([\s\-\(\)]{,2}\d){5,10}\d'),
+ ' [FILTERED]', result)
else:
result = re.sub(r'(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d',
'[PHONE]', result)
return result
class Validations():
@staticmethod
def is_email(value):
return re.match(r'^[a-zA-z0-9](\w|[\.\-\+]){,200}@'
'([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_phone(value):
return re.match(r'^(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d$', value)
@staticmethod
def is_hostname(value):
return re.match(r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_ip_address(value):
ip_range = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
.format(ip=ip_range), value)
@staticmethod
def is_number(value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
@staticmethod
def is_integer(value):
return re.match(r'^-?0?[1-9]\d*$', value)
@staticmethod
def is_date(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])$', value)
@staticmethod
def is_time(value):
return re.match(r'^(0\d|1[\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)
@staticmethod
def is_datetime(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])'
'(\s|T)'
'(0\d|1\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)

Драгомир обнови решението на 16.04.2014 20:54 (преди около 10 години)

import re
class PrivacyFilter():
- def __init__(this, text):
- this.text = text
- this.preserve_phone_country_code = False
- this.preserve_email_hostname = False
- this.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(this):
- if this.preserve_email_hostname:
+ def filtered(self):
+ if self.preserve_email_hostname:
result = re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
'(?=@([a-zA-Z0-9]'
'([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
- '[FILTERED]', this.text)
- elif this.partially_preserve_email_username:
+ '[FILTERED]', self.text)
+ elif self.partially_preserve_email_username:
result = re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
'[a-zA-Z0-9\.\-\_\+]{,197}'
'(?=@([a-zA-Z0-9]'
'([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
- '[FILTERED]', this.text)
+ '[FILTERED]', self.text)
else:
result = re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
'([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?',
- '[EMAIL]', this.text)
- if this.preserve_phone_country_code:
+ '[EMAIL]', self.text)
+ if self.preserve_phone_country_code:
for times in range(2, -1, -1):
result = re.sub('{}{}{}{}{}'
.format(r'((?<=00[1-9]\d{', times,
'})|(?<=\+[1-9]\d{', times,
'}))([\s\-\(\)]{,2}\d){5,10}\d'),
' [FILTERED]', result)
else:
result = re.sub(r'(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d',
'[PHONE]', result)
return result
class Validations():
@staticmethod
def is_email(value):
return re.match(r'^[a-zA-z0-9](\w|[\.\-\+]){,200}@'
'([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_phone(value):
return re.match(r'^(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d$', value)
@staticmethod
def is_hostname(value):
return re.match(r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
@staticmethod
def is_ip_address(value):
ip_range = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
.format(ip=ip_range), value)
@staticmethod
def is_number(value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
@staticmethod
def is_integer(value):
return re.match(r'^-?0?[1-9]\d*$', value)
@staticmethod
def is_date(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])$', value)
@staticmethod
def is_time(value):
return re.match(r'^(0\d|1[\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)
@staticmethod
def is_datetime(value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])'
'(\s|T)'
'(0\d|1\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)
  • Използваш един и същ регулярен израз на няколко места. Искаш да го изнесеш като константа :)

  • Опрости малко filtered метода. Станал е огромен с много if-elif-elif-... Напълно в реда на нещата е да си дефинираш няколко protected метода, които да вършат мръсната работа и filtered само да решава кой от тях да извика

  • В условието на задачата пише, методите в Validations да са класови, не статични

Драгомир обнови решението на 18.04.2014 15:42 (преди около 10 години)

import re
+# TODO: make dict with constants for regular expressions
+
+hostname = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
+email = '{}{}'.format('[a-zA-z0-9](\w|[\.\-\+]){,200}@', hostname)
+
+
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):
+ def _preserve_email_hostname(self):
+ return re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
+ '(?=@([a-zA-Z0-9]'
+ '([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
+ '[FILTERED]', self.text)
+
+ def _partially_preserve_email_username(self):
+ return re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
+ '[a-zA-Z0-9\.\-\_\+]{,197}'
+ '(?=@([a-zA-Z0-9]'
+ '([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
+ '[FILTERED]', self.text)
+
+ def _filter_entire_email(self):
+ return re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
+ '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?',
+ '[EMAIL]', self.text)
+
+ def _preserve_phone_country_code(self, result):
+ for times in range(2, -1, -1):
+ result = re.sub('{}{}{}{}{}'
+ .format(r'((?<=00[1-9]\d{', times,
+ '})|(?<=\+[1-9]\d{', times,
+ '}))([\s\-\(\)]{,2}\d){5,10}\d'),
+ ' [FILTERED]', result)
+ return result
+
+ def _filter_phone_number(self, result):
+ return re.sub(r'(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
+ '([\s\-\(\)]{,2}\d){5,10}\d',
+ '[PHONE]', result)
+
+ def _filter_email(self):
if self.preserve_email_hostname:
- result = re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
- '(?=@([a-zA-Z0-9]'
- '([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
- '[FILTERED]', self.text)
+ result = self._preserve_email_hostname()
+
elif self.partially_preserve_email_username:
- result = re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
- '[a-zA-Z0-9\.\-\_\+]{,197}'
- '(?=@([a-zA-Z0-9]'
- '([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
- '[FILTERED]', self.text)
+ result = self._partially_preserve_email_username()
else:
- result = re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
- '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?',
- '[EMAIL]', self.text)
+ result = self._filter_entire_email()
+ return result
+
+ def _filter_phone(self, result):
if self.preserve_phone_country_code:
- for times in range(2, -1, -1):
- result = re.sub('{}{}{}{}{}'
- .format(r'((?<=00[1-9]\d{', times,
- '})|(?<=\+[1-9]\d{', times,
- '}))([\s\-\(\)]{,2}\d){5,10}\d'),
- ' [FILTERED]', result)
+ result = self._preserve_phone_country_code(result)
else:
- result = re.sub(r'(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
- '([\s\-\(\)]{,2}\d){5,10}\d',
- '[PHONE]', result)
+ result = self._filter_phone_number(result)
return result
+ def filtered(self):
+ result = self._filter_email()
+ result = self._filter_phone(result)
+ return result
+
class Validations():
- @staticmethod
- def is_email(value):
- return re.match(r'^[a-zA-z0-9](\w|[\.\-\+]){,200}@'
- '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
+ @classmethod
+ def is_email(self, value):
+ return re.match(r'^{}'.format(email), value)
- @staticmethod
- def is_phone(value):
+ @classmethod
+ def is_phone(self, value):
return re.match(r'^(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d$', value)
- @staticmethod
- def is_hostname(value):
- return re.match(r'^([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$', value)
+ @classmethod
+ def is_hostname(self, value):
+ return re.match(r'^{}'.format(hostname), value)
- @staticmethod
- def is_ip_address(value):
+ @classmethod
+ def is_ip_address(self, value):
ip_range = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
.format(ip=ip_range), value)
- @staticmethod
- def is_number(value):
+ @classmethod
+ def is_number(self, value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
- @staticmethod
- def is_integer(value):
+ @classmethod
+ def is_integer(self, value):
return re.match(r'^-?0?[1-9]\d*$', value)
- @staticmethod
- def is_date(value):
+ @classmethod
+ def is_date(self, value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])$', value)
- @staticmethod
- def is_time(value):
+ @classmethod
+ def is_time(self, value):
return re.match(r'^(0\d|1[\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)
- @staticmethod
- def is_datetime(value):
+ @classmethod
+ def is_datetime(self, value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])'
'(\s|T)'
'(0\d|1\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)

Драгомир обнови решението на 18.04.2014 15:47 (преди около 10 години)

import re
# TODO: make dict with constants for regular expressions
hostname = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
email = '{}{}'.format('[a-zA-z0-9](\w|[\.\-\+]){,200}@', hostname)
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 _preserve_email_hostname(self):
return re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
'(?=@([a-zA-Z0-9]'
'([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
return re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
'[a-zA-Z0-9\.\-\_\+]{,197}'
'(?=@([a-zA-Z0-9]'
'([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
'[FILTERED]', self.text)
def _filter_entire_email(self):
return re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
'([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?',
'[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
for times in range(2, -1, -1):
result = re.sub('{}{}{}{}{}'
.format(r'((?<=00[1-9]\d{', times,
'})|(?<=\+[1-9]\d{', times,
'}))([\s\-\(\)]{,2}\d){5,10}\d'),
' [FILTERED]', result)
return result
def _filter_phone_number(self, result):
return re.sub(r'(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d',
'[PHONE]', result)
def _filter_email(self):
if self.preserve_email_hostname:
- result = self._preserve_email_hostname()
-
+ return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
- result = self._partially_preserve_email_username()
+ return self._partially_preserve_email_username()
else:
- result = self._filter_entire_email()
- return result
+ return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
- result = self._preserve_phone_country_code(result)
+ return self._preserve_phone_country_code(result)
else:
- result = self._filter_phone_number(result)
- return result
+ return self._filter_phone_number(result)
def filtered(self):
- result = self._filter_email()
- result = self._filter_phone(result)
- return result
+ return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
return re.match(r'^{}'.format(email), value)
@classmethod
def is_phone(self, value):
return re.match(r'^(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
'([\s\-\(\)]{,2}\d){5,10}\d$', value)
@classmethod
def is_hostname(self, value):
return re.match(r'^{}'.format(hostname), value)
@classmethod
def is_ip_address(self, value):
ip_range = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
.format(ip=ip_range), value)
@classmethod
def is_number(self, value):
return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
@classmethod
def is_integer(self, value):
return re.match(r'^-?0?[1-9]\d*$', value)
@classmethod
def is_date(self, value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])$', value)
@classmethod
def is_time(self, value):
return re.match(r'^(0\d|1[\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)
@classmethod
def is_datetime(self, value):
return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
'([1-9]|[1-2]\d|3[0-1])'
'(\s|T)'
'(0\d|1\d|2[0-3]):'
'[0-5]\d:[0-5]\d$', value)

Драгомир обнови решението на 18.04.2014 16:36 (преди около 10 години)

import re
-# TODO: make dict with constants for regular expressions
-
-hostname = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
+HOSTNAME = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
-email = '{}{}'.format('[a-zA-z0-9](\w|[\.\-\+]){,200}@', hostname)
+EMAIL_ADDRESS = '[a-zA-z0-9](\w|[\.\-\+])'
+EMAIL = '{}{}@{}'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME)
+PHONE_CODE = '(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
+PHONE_NUMBER = '([\s\-\(\)]{,2}\d){5,10}\d'
+IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
+INTEGER = '-?0?[1-9]\d*'
+DATE = '\d{4}\-([1-9]|1[0-2])\-([1-9]|[1-2]\d|3[0-1])'
+TIME = '(0\d|1[\d|2[0-3]):[0-5]\d:[0-5]\d'
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 _preserve_email_hostname(self):
- return re.sub(r'([a-zA-z0-9](\w|[\.\-\+]){,200})'
- '(?=@([a-zA-Z0-9]'
- '([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
+ return re.sub(r'{}{}(?=@{})'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME),
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
- return re.sub(r'(?<=[a-zA-Z0-9](\w|[\.\-\+]){2})'
- '[a-zA-Z0-9\.\-\_\+]{,197}'
- '(?=@([a-zA-Z0-9]'
- '([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?)',
+ return re.sub(r'(?<={}{}){}{}(?=@{})'.format(EMAIL_ADDRESS, '{2}',
+ EMAIL_ADDRESS, '{,197}',
+ HOSTNAME),
'[FILTERED]', self.text)
def _filter_entire_email(self):
- return re.sub(r'[a-zA-z0-9][a-zA-Z0-9\.\-\_\+]{,200}@'
- '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?',
- '[EMAIL]', self.text)
+ return re.sub(r'{}'.format(EMAIL), '[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
for times in range(2, -1, -1):
- result = re.sub('{}{}{}{}{}'
+ result = re.sub('{}{}{}{}{}{}'
.format(r'((?<=00[1-9]\d{', times,
'})|(?<=\+[1-9]\d{', times,
- '}))([\s\-\(\)]{,2}\d){5,10}\d'),
+ '}))', PHONE_NUMBER),
' [FILTERED]', result)
return result
def _filter_phone_number(self, result):
- return re.sub(r'(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
- '([\s\-\(\)]{,2}\d){5,10}\d',
+ return re.sub(r'{}{}'.format(PHONE_CODE, PHONE_NUMBER),
'[PHONE]', result)
def _filter_email(self):
if self.preserve_email_hostname:
return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
return self._partially_preserve_email_username()
else:
return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
return self._preserve_phone_country_code(result)
else:
return self._filter_phone_number(result)
def filtered(self):
return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
- return re.match(r'^{}'.format(email), value)
+ return re.match(r'^{}$'.format(EMAIL), value)
@classmethod
def is_phone(self, value):
- return re.match(r'^(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
- '([\s\-\(\)]{,2}\d){5,10}\d$', value)
+ return re.match(r'^{}{}$'.format(PHONE_CODE, PHONE_NUMBER), value)
@classmethod
def is_hostname(self, value):
- return re.match(r'^{}'.format(hostname), value)
+ return re.match(r'^{}$'.format(HOSTNAME), value)
@classmethod
def is_ip_address(self, value):
- ip_range = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
- return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'
- .format(ip=ip_range), value)
+ return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'.format(ip=IP_RANGE), value)
@classmethod
def is_number(self, value):
- return re.match(r'^-?0?[1-9]\d*\.?\d*$', value)
+ return re.match(r'^{}\.?\d*$'.format(INTEGER), value)
@classmethod
def is_integer(self, value):
- return re.match(r'^-?0?[1-9]\d*$', value)
+ return re.match(r'^{}$'.format(INTEGER), value)
@classmethod
def is_date(self, value):
- return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
- '([1-9]|[1-2]\d|3[0-1])$', value)
+ return re.match(r'^{}$'.format(DATE), value)
@classmethod
def is_time(self, value):
- return re.match(r'^(0\d|1[\d|2[0-3]):'
- '[0-5]\d:[0-5]\d$', value)
+ return re.match(r'^{}$'.format(TIME), value)
@classmethod
def is_datetime(self, value):
- return re.match(r'^\d{4}\-([1-9]|1[0-2])\-'
- '([1-9]|[1-2]\d|3[0-1])'
+ return re.match(r'^{}(\s|T){}$'.format(DATE, TIME), value)
- '(\s|T)'
- '(0\d|1\d|2[0-3]):'
- '[0-5]\d:[0-5]\d$', value)

Може ли някаква подсказка за международния код? Не виждам как мога да го направя да разпознава, че +359 е кода, а не +3 или +35, за примерно +359123456789. При +3 има точно 11 цифри, при +35 са 10, при +359, съответно 9, и трите случая отговарят на Същинска част: от 6 до 11 цифри.

Единственото, което ми идва на ум е да се проверят всички валидни международни кодове и да се сложат в един регулярен израз, който да разпознава само тях, но все си мисля, че трябва да има и по-лесен начин?

Драгомир обнови решението на 18.04.2014 18:50 (преди около 10 години)

import re
+# Phone codes taken from http://countrycode.org/
+
HOSTNAME = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
EMAIL_ADDRESS = '[a-zA-z0-9](\w|[\.\-\+])'
EMAIL = '{}{}@{}'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME)
-PHONE_CODE = '(0[1-9]|00[1-9]\d{,3}|\+[1-9]\d{,3})'
+ONE_DIGIT_CODE = '1|7'
+TWO_DIGITS_CODE = '20|3[0149]|4([01]|[3-9])|5[1-8]|6[0-6]|8[1246]|9([0-5]|8)'
+THREE_DIGITS_CODE = '21[2368]|22\d|23\d|24([0-5]|[89])|25[0-8]|26\d|29[01789]'\
+ '|35\d|37[0-8]|38[012579]|42[013]|50\d|59([0-3]|[5789])'\
+ '|67(0|[2-9])|68([0-3]|[5-9])|69[0-2]|85[02356]|870'\
+ '|88[06]|96[0-8]|97[0-7]|99([2-6]|8)'
+PHONE_CODE = '(0|{}|{}|{})'.format(ONE_DIGIT_CODE,
+ TWO_DIGITS_CODE,
+ THREE_DIGITS_CODE)
PHONE_NUMBER = '([\s\-\(\)]{,2}\d){5,10}\d'
+PHONE_FULL = '(((00|\+){})|0){}'.format(PHONE_CODE, PHONE_NUMBER)
IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
INTEGER = '-?0?[1-9]\d*'
DATE = '\d{4}\-([1-9]|1[0-2])\-([1-9]|[1-2]\d|3[0-1])'
TIME = '(0\d|1[\d|2[0-3]):[0-5]\d:[0-5]\d'
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 _preserve_email_hostname(self):
return re.sub(r'{}{}(?=@{})'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME),
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
return re.sub(r'(?<={}{}){}{}(?=@{})'.format(EMAIL_ADDRESS, '{2}',
EMAIL_ADDRESS, '{,197}',
HOSTNAME),
'[FILTERED]', self.text)
def _filter_entire_email(self):
return re.sub(r'{}'.format(EMAIL), '[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
- for times in range(2, -1, -1):
- result = re.sub('{}{}{}{}{}{}'
- .format(r'((?<=00[1-9]\d{', times,
- '})|(?<=\+[1-9]\d{', times,
- '}))', PHONE_NUMBER),
- ' [FILTERED]', result)
- return result
+ return re.sub(r'((?<=\+({one_digit}))|(?<=\+({two_digits}))|'
+ '(?<=\+({three_digits}))|(?<=00({one_digit}))|'
+ '(?<=00({two_digits}))|(?<=00({three_digits})))'
+ '{phone_number}'.format(one_digit=ONE_DIGIT_CODE,
+ two_digits=TWO_DIGITS_CODE,
+ three_digits=THREE_DIGITS_CODE,
+ phone_number=PHONE_NUMBER),
+ ' [FILTERED]', result)
def _filter_phone_number(self, result):
- return re.sub(r'{}{}'.format(PHONE_CODE, PHONE_NUMBER),
- '[PHONE]', result)
+ return re.sub(r'{}'.format(PHONE_FULL), '[PHONE]', result)
def _filter_email(self):
if self.preserve_email_hostname:
return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
return self._partially_preserve_email_username()
else:
return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
return self._preserve_phone_country_code(result)
else:
return self._filter_phone_number(result)
def filtered(self):
return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
return re.match(r'^{}$'.format(EMAIL), value)
@classmethod
def is_phone(self, value):
- return re.match(r'^{}{}$'.format(PHONE_CODE, PHONE_NUMBER), value)
+ return re.match(r'^{}$'.format(PHONE_FULL), value)
@classmethod
def is_hostname(self, value):
return re.match(r'^{}$'.format(HOSTNAME), value)
@classmethod
def is_ip_address(self, value):
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'.format(ip=IP_RANGE), value)
@classmethod
def is_number(self, value):
return re.match(r'^{}\.?\d*$'.format(INTEGER), value)
@classmethod
def is_integer(self, value):
return re.match(r'^{}$'.format(INTEGER), value)
@classmethod
def is_date(self, value):
return re.match(r'^{}$'.format(DATE), value)
@classmethod
def is_time(self, value):
return re.match(r'^{}$'.format(TIME), value)
@classmethod
def is_datetime(self, value):
return re.match(r'^{}(\s|T){}$'.format(DATE, TIME), value)

Драгомир обнови решението на 18.04.2014 18:54 (преди около 10 години)

import re
# Phone codes taken from http://countrycode.org/
HOSTNAME = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
EMAIL_ADDRESS = '[a-zA-z0-9](\w|[\.\-\+])'
EMAIL = '{}{}@{}'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME)
ONE_DIGIT_CODE = '1|7'
TWO_DIGITS_CODE = '20|3[0149]|4([01]|[3-9])|5[1-8]|6[0-6]|8[1246]|9([0-5]|8)'
THREE_DIGITS_CODE = '21[2368]|22\d|23\d|24([0-5]|[89])|25[0-8]|26\d|29[01789]'\
'|35\d|37[0-8]|38[012579]|42[013]|50\d|59([0-3]|[5789])'\
'|67(0|[2-9])|68([0-3]|[5-9])|69[0-2]|85[02356]|870'\
'|88[06]|96[0-8]|97[0-7]|99([2-6]|8)'
-PHONE_CODE = '(0|{}|{}|{})'.format(ONE_DIGIT_CODE,
- TWO_DIGITS_CODE,
- THREE_DIGITS_CODE)
+PHONE_CODE = '(0|((00|\+)({}|{}|{})))'.format(ONE_DIGIT_CODE,
+ TWO_DIGITS_CODE,
+ THREE_DIGITS_CODE)
PHONE_NUMBER = '([\s\-\(\)]{,2}\d){5,10}\d'
-PHONE_FULL = '(((00|\+){})|0){}'.format(PHONE_CODE, PHONE_NUMBER)
+PHONE_FULL = '{}{}'.format(PHONE_CODE, PHONE_NUMBER)
IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
INTEGER = '-?0?[1-9]\d*'
DATE = '\d{4}\-([1-9]|1[0-2])\-([1-9]|[1-2]\d|3[0-1])'
TIME = '(0\d|1[\d|2[0-3]):[0-5]\d:[0-5]\d'
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 _preserve_email_hostname(self):
return re.sub(r'{}{}(?=@{})'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME),
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
return re.sub(r'(?<={}{}){}{}(?=@{})'.format(EMAIL_ADDRESS, '{2}',
EMAIL_ADDRESS, '{,197}',
HOSTNAME),
'[FILTERED]', self.text)
def _filter_entire_email(self):
return re.sub(r'{}'.format(EMAIL), '[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
return re.sub(r'((?<=\+({one_digit}))|(?<=\+({two_digits}))|'
'(?<=\+({three_digits}))|(?<=00({one_digit}))|'
'(?<=00({two_digits}))|(?<=00({three_digits})))'
'{phone_number}'.format(one_digit=ONE_DIGIT_CODE,
two_digits=TWO_DIGITS_CODE,
three_digits=THREE_DIGITS_CODE,
phone_number=PHONE_NUMBER),
' [FILTERED]', result)
def _filter_phone_number(self, result):
return re.sub(r'{}'.format(PHONE_FULL), '[PHONE]', result)
def _filter_email(self):
if self.preserve_email_hostname:
return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
return self._partially_preserve_email_username()
else:
return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
return self._preserve_phone_country_code(result)
else:
return self._filter_phone_number(result)
def filtered(self):
return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
return re.match(r'^{}$'.format(EMAIL), value)
@classmethod
def is_phone(self, value):
return re.match(r'^{}$'.format(PHONE_FULL), value)
@classmethod
def is_hostname(self, value):
return re.match(r'^{}$'.format(HOSTNAME), value)
@classmethod
def is_ip_address(self, value):
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'.format(ip=IP_RANGE), value)
@classmethod
def is_number(self, value):
return re.match(r'^{}\.?\d*$'.format(INTEGER), value)
@classmethod
def is_integer(self, value):
return re.match(r'^{}$'.format(INTEGER), value)
@classmethod
def is_date(self, value):
return re.match(r'^{}$'.format(DATE), value)
@classmethod
def is_time(self, value):
return re.match(r'^{}$'.format(TIME), value)
@classmethod
def is_datetime(self, value):
return re.match(r'^{}(\s|T){}$'.format(DATE, TIME), value)

Драгомир обнови решението на 18.04.2014 19:09 (преди около 10 години)

import re
# Phone codes taken from http://countrycode.org/
HOSTNAME = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
EMAIL_ADDRESS = '[a-zA-z0-9](\w|[\.\-\+])'
EMAIL = '{}{}@{}'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME)
ONE_DIGIT_CODE = '1|7'
TWO_DIGITS_CODE = '20|3[0149]|4([01]|[3-9])|5[1-8]|6[0-6]|8[1246]|9([0-5]|8)'
THREE_DIGITS_CODE = '21[2368]|22\d|23\d|24([0-5]|[89])|25[0-8]|26\d|29[01789]'\
'|35\d|37[0-8]|38[012579]|42[013]|50\d|59([0-3]|[5789])'\
'|67(0|[2-9])|68([0-3]|[5-9])|69[0-2]|85[02356]|870'\
'|88[06]|96[0-8]|97[0-7]|99([2-6]|8)'
PHONE_CODE = '(0|((00|\+)({}|{}|{})))'.format(ONE_DIGIT_CODE,
TWO_DIGITS_CODE,
THREE_DIGITS_CODE)
PHONE_NUMBER = '([\s\-\(\)]{,2}\d){5,10}\d'
PHONE_FULL = '{}{}'.format(PHONE_CODE, PHONE_NUMBER)
-IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4][\d|25[0-5])'
-INTEGER = '-?0?[1-9]\d*'
+IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4]\d|25[0-5])'
+INTEGER = '-?0?([1-9]\d*)?'
DATE = '\d{4}\-([1-9]|1[0-2])\-([1-9]|[1-2]\d|3[0-1])'
TIME = '(0\d|1[\d|2[0-3]):[0-5]\d:[0-5]\d'
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 _preserve_email_hostname(self):
return re.sub(r'{}{}(?=@{})'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME),
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
return re.sub(r'(?<={}{}){}{}(?=@{})'.format(EMAIL_ADDRESS, '{2}',
EMAIL_ADDRESS, '{,197}',
HOSTNAME),
'[FILTERED]', self.text)
def _filter_entire_email(self):
return re.sub(r'{}'.format(EMAIL), '[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
return re.sub(r'((?<=\+({one_digit}))|(?<=\+({two_digits}))|'
'(?<=\+({three_digits}))|(?<=00({one_digit}))|'
'(?<=00({two_digits}))|(?<=00({three_digits})))'
'{phone_number}'.format(one_digit=ONE_DIGIT_CODE,
two_digits=TWO_DIGITS_CODE,
three_digits=THREE_DIGITS_CODE,
phone_number=PHONE_NUMBER),
' [FILTERED]', result)
def _filter_phone_number(self, result):
return re.sub(r'{}'.format(PHONE_FULL), '[PHONE]', result)
def _filter_email(self):
if self.preserve_email_hostname:
return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
return self._partially_preserve_email_username()
else:
return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
return self._preserve_phone_country_code(result)
else:
return self._filter_phone_number(result)
def filtered(self):
return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
return re.match(r'^{}$'.format(EMAIL), value)
@classmethod
def is_phone(self, value):
return re.match(r'^{}$'.format(PHONE_FULL), value)
@classmethod
def is_hostname(self, value):
return re.match(r'^{}$'.format(HOSTNAME), value)
@classmethod
def is_ip_address(self, value):
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'.format(ip=IP_RANGE), value)
@classmethod
def is_number(self, value):
return re.match(r'^{}\.?\d*$'.format(INTEGER), value)
@classmethod
def is_integer(self, value):
return re.match(r'^{}$'.format(INTEGER), value)
@classmethod
def is_date(self, value):
return re.match(r'^{}$'.format(DATE), value)
@classmethod
def is_time(self, value):
return re.match(r'^{}$'.format(TIME), value)
@classmethod
def is_datetime(self, value):
return re.match(r'^{}(\s|T){}$'.format(DATE, TIME), value)

Драгомир обнови решението на 19.04.2014 21:22 (преди около 10 години)

import re
-# Phone codes taken from http://countrycode.org/
-
HOSTNAME = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
EMAIL_ADDRESS = '[a-zA-z0-9](\w|[\.\-\+])'
EMAIL = '{}{}@{}'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME)
-ONE_DIGIT_CODE = '1|7'
-TWO_DIGITS_CODE = '20|3[0149]|4([01]|[3-9])|5[1-8]|6[0-6]|8[1246]|9([0-5]|8)'
-THREE_DIGITS_CODE = '21[2368]|22\d|23\d|24([0-5]|[89])|25[0-8]|26\d|29[01789]'\
- '|35\d|37[0-8]|38[012579]|42[013]|50\d|59([0-3]|[5789])'\
- '|67(0|[2-9])|68([0-3]|[5-9])|69[0-2]|85[02356]|870'\
- '|88[06]|96[0-8]|97[0-7]|99([2-6]|8)'
-PHONE_CODE = '(0|((00|\+)({}|{}|{})))'.format(ONE_DIGIT_CODE,
- TWO_DIGITS_CODE,
- THREE_DIGITS_CODE)
+PHONE_CODE = '(0|((00|\+)[1-9](\d{2}|\d)?))'
PHONE_NUMBER = '([\s\-\(\)]{,2}\d){5,10}\d'
PHONE_FULL = '{}{}'.format(PHONE_CODE, PHONE_NUMBER)
IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4]\d|25[0-5])'
INTEGER = '-?0?([1-9]\d*)?'
DATE = '\d{4}\-([1-9]|1[0-2])\-([1-9]|[1-2]\d|3[0-1])'
TIME = '(0\d|1[\d|2[0-3]):[0-5]\d:[0-5]\d'
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 _preserve_email_hostname(self):
return re.sub(r'{}{}(?=@{})'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME),
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
return re.sub(r'(?<={}{}){}{}(?=@{})'.format(EMAIL_ADDRESS, '{2}',
EMAIL_ADDRESS, '{,197}',
HOSTNAME),
'[FILTERED]', self.text)
def _filter_entire_email(self):
return re.sub(r'{}'.format(EMAIL), '[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
- return re.sub(r'((?<=\+({one_digit}))|(?<=\+({two_digits}))|'
- '(?<=\+({three_digits}))|(?<=00({one_digit}))|'
- '(?<=00({two_digits}))|(?<=00({three_digits})))'
- '{phone_number}'.format(one_digit=ONE_DIGIT_CODE,
- two_digits=TWO_DIGITS_CODE,
- three_digits=THREE_DIGITS_CODE,
- phone_number=PHONE_NUMBER),
- ' [FILTERED]', result)
+ for times in range(2, -1, -1):
+ result = re.sub('{}{}{}{}{}{}'
+ .format(r'((?<=00[1-9]\d{', times,
+ '})|(?<=\+[1-9]\d{', times,
+ '}))', PHONE_NUMBER),
+ ' [FILTERED]', result)
+ return result
def _filter_phone_number(self, result):
return re.sub(r'{}'.format(PHONE_FULL), '[PHONE]', result)
def _filter_email(self):
if self.preserve_email_hostname:
return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
return self._partially_preserve_email_username()
else:
return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
return self._preserve_phone_country_code(result)
else:
return self._filter_phone_number(result)
def filtered(self):
return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
return re.match(r'^{}$'.format(EMAIL), value)
@classmethod
def is_phone(self, value):
return re.match(r'^{}$'.format(PHONE_FULL), value)
@classmethod
def is_hostname(self, value):
return re.match(r'^{}$'.format(HOSTNAME), value)
@classmethod
def is_ip_address(self, value):
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'.format(ip=IP_RANGE), value)
@classmethod
def is_number(self, value):
return re.match(r'^{}\.?\d*$'.format(INTEGER), value)
@classmethod
def is_integer(self, value):
return re.match(r'^{}$'.format(INTEGER), value)
@classmethod
def is_date(self, value):
return re.match(r'^{}$'.format(DATE), value)
@classmethod
def is_time(self, value):
return re.match(r'^{}$'.format(TIME), value)
@classmethod
def is_datetime(self, value):
return re.match(r'^{}(\s|T){}$'.format(DATE, TIME), value)

Драгомир обнови решението на 20.04.2014 14:46 (преди около 10 години)

import re
HOSTNAME = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
- '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?$'
-EMAIL_ADDRESS = '[a-zA-z0-9](\w|[\.\-\+])'
-EMAIL = '{}{}@{}'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME)
+ '[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
+EMAIL_ADDRESS_SYMBOLS = '(\w|[\.\-\+])'
+EMAIL_ADDRESS = '[a-zA-Z0-9]{}{}@{}'\
+ .format(EMAIL_ADDRESS_SYMBOLS, '{,200}', HOSTNAME)
PHONE_CODE = '(0|((00|\+)[1-9](\d{2}|\d)?))'
PHONE_NUMBER = '([\s\-\(\)]{,2}\d){5,10}\d'
PHONE_FULL = '{}{}'.format(PHONE_CODE, PHONE_NUMBER)
IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4]\d|25[0-5])'
INTEGER = '-?0?([1-9]\d*)?'
DATE = '\d{4}\-([1-9]|1[0-2])\-([1-9]|[1-2]\d|3[0-1])'
TIME = '(0\d|1[\d|2[0-3]):[0-5]\d:[0-5]\d'
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 _preserve_email_hostname(self):
- return re.sub(r'{}{}(?=@{})'.format(EMAIL_ADDRESS, '{,200}', HOSTNAME),
+ return re.sub(r'{}{}{}(?=@{})'.format('[a-zA-Z0-9]',
+ EMAIL_ADDRESS_SYMBOLS,
+ '{,200}', HOSTNAME),
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
- return re.sub(r'(?<={}{}){}{}(?=@{})'.format(EMAIL_ADDRESS, '{2}',
- EMAIL_ADDRESS, '{,197}',
- HOSTNAME),
- '[FILTERED]', self.text)
+ result = re.sub(r'(?<=\W){}{}{}(?=@{})'
+ .format('[a-zA-Z0-9]',
+ EMAIL_ADDRESS_SYMBOLS, '{,4}',
+ HOSTNAME),
+ '[FILTERED]', self.text)
+ return re.sub(r'(?<={}{}{}){}{}(?=@{})'
+ .format('[a-zA-Z0-9]',
+ EMAIL_ADDRESS_SYMBOLS, '{2}',
+ EMAIL_ADDRESS_SYMBOLS, '{,197}',
+ HOSTNAME),
+ '[FILTERED]', result)
def _filter_entire_email(self):
- return re.sub(r'{}'.format(EMAIL), '[EMAIL]', self.text)
+ return re.sub(r'{}'.format(EMAIL_ADDRESS), '[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
for times in range(2, -1, -1):
result = re.sub('{}{}{}{}{}{}'
.format(r'((?<=00[1-9]\d{', times,
'})|(?<=\+[1-9]\d{', times,
'}))', PHONE_NUMBER),
' [FILTERED]', result)
+ result = self._filter_phone_number(result, '[FILTERED]')
return result
- def _filter_phone_number(self, result):
- return re.sub(r'{}'.format(PHONE_FULL), '[PHONE]', result)
+ def _filter_phone_number(self, result, filtered='[PHONE]'):
+ return re.sub(r'{}'.format(PHONE_FULL), filtered, result)
def _filter_email(self):
if self.preserve_email_hostname:
return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
return self._partially_preserve_email_username()
else:
return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
return self._preserve_phone_country_code(result)
else:
return self._filter_phone_number(result)
def filtered(self):
return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
- return re.match(r'^{}$'.format(EMAIL), value)
+ return re.match(r'^{}$'.format(EMAIL_ADDRESS), value)
@classmethod
def is_phone(self, value):
return re.match(r'^{}$'.format(PHONE_FULL), value)
@classmethod
def is_hostname(self, value):
return re.match(r'^{}$'.format(HOSTNAME), value)
@classmethod
def is_ip_address(self, value):
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'.format(ip=IP_RANGE), value)
@classmethod
def is_number(self, value):
return re.match(r'^{}\.?\d*$'.format(INTEGER), value)
@classmethod
def is_integer(self, value):
return re.match(r'^{}$'.format(INTEGER), value)
@classmethod
def is_date(self, value):
return re.match(r'^{}$'.format(DATE), value)
@classmethod
def is_time(self, value):
return re.match(r'^{}$'.format(TIME), value)
@classmethod
def is_datetime(self, value):
return re.match(r'^{}(\s|T){}$'.format(DATE, TIME), value)

Драгомир обнови решението на 20.04.2014 14:50 (преди около 10 години)

import re
HOSTNAME = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
EMAIL_ADDRESS_SYMBOLS = '(\w|[\.\-\+])'
EMAIL_ADDRESS = '[a-zA-Z0-9]{}{}@{}'\
.format(EMAIL_ADDRESS_SYMBOLS, '{,200}', HOSTNAME)
PHONE_CODE = '(0|((00|\+)[1-9](\d{2}|\d)?))'
PHONE_NUMBER = '([\s\-\(\)]{,2}\d){5,10}\d'
PHONE_FULL = '{}{}'.format(PHONE_CODE, PHONE_NUMBER)
IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4]\d|25[0-5])'
INTEGER = '-?0?([1-9]\d*)?'
DATE = '\d{4}\-([1-9]|1[0-2])\-([1-9]|[1-2]\d|3[0-1])'
TIME = '(0\d|1[\d|2[0-3]):[0-5]\d:[0-5]\d'
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 _preserve_email_hostname(self):
return re.sub(r'{}{}{}(?=@{})'.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS,
'{,200}', HOSTNAME),
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
- result = re.sub(r'(?<=\W){}{}{}(?=@{})'
+ result = re.sub(r'(?<=[^\w\.\-\+]){}{}{}(?=@{})'
.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS, '{,4}',
HOSTNAME),
'[FILTERED]', self.text)
return re.sub(r'(?<={}{}{}){}{}(?=@{})'
.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS, '{2}',
EMAIL_ADDRESS_SYMBOLS, '{,197}',
HOSTNAME),
'[FILTERED]', result)
def _filter_entire_email(self):
return re.sub(r'{}'.format(EMAIL_ADDRESS), '[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
for times in range(2, -1, -1):
result = re.sub('{}{}{}{}{}{}'
.format(r'((?<=00[1-9]\d{', times,
'})|(?<=\+[1-9]\d{', times,
'}))', PHONE_NUMBER),
' [FILTERED]', result)
result = self._filter_phone_number(result, '[FILTERED]')
return result
def _filter_phone_number(self, result, filtered='[PHONE]'):
return re.sub(r'{}'.format(PHONE_FULL), filtered, result)
def _filter_email(self):
if self.preserve_email_hostname:
return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
return self._partially_preserve_email_username()
else:
return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
return self._preserve_phone_country_code(result)
else:
return self._filter_phone_number(result)
def filtered(self):
return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
return re.match(r'^{}$'.format(EMAIL_ADDRESS), value)
@classmethod
def is_phone(self, value):
return re.match(r'^{}$'.format(PHONE_FULL), value)
@classmethod
def is_hostname(self, value):
return re.match(r'^{}$'.format(HOSTNAME), value)
@classmethod
def is_ip_address(self, value):
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'.format(ip=IP_RANGE), value)
@classmethod
def is_number(self, value):
return re.match(r'^{}\.?\d*$'.format(INTEGER), value)
@classmethod
def is_integer(self, value):
return re.match(r'^{}$'.format(INTEGER), value)
@classmethod
def is_date(self, value):
return re.match(r'^{}$'.format(DATE), value)
@classmethod
def is_time(self, value):
return re.match(r'^{}$'.format(TIME), value)
@classmethod
def is_datetime(self, value):
return re.match(r'^{}(\s|T){}$'.format(DATE, TIME), value)

Драгомир обнови решението на 20.04.2014 22:58 (преди около 10 години)

import re
HOSTNAME = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
EMAIL_ADDRESS_SYMBOLS = '(\w|[\.\-\+])'
EMAIL_ADDRESS = '[a-zA-Z0-9]{}{}@{}'\
.format(EMAIL_ADDRESS_SYMBOLS, '{,200}', HOSTNAME)
PHONE_CODE = '(0|((00|\+)[1-9](\d{2}|\d)?))'
-PHONE_NUMBER = '([\s\-\(\)]{,2}\d){5,10}\d'
+PHONE_SEPARATORS = '[\s\-\(\)]{,2}'
+PHONE_NUMBER = '({}\d){}({})?\d'\
+ .format(PHONE_SEPARATORS, '{5,10}', PHONE_SEPARATORS)
PHONE_FULL = '{}{}'.format(PHONE_CODE, PHONE_NUMBER)
IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4]\d|25[0-5])'
INTEGER = '-?0?([1-9]\d*)?'
DATE = '\d{4}\-([1-9]|1[0-2])\-([1-9]|[1-2]\d|3[0-1])'
TIME = '(0\d|1[\d|2[0-3]):[0-5]\d:[0-5]\d'
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 _preserve_email_hostname(self):
return re.sub(r'{}{}{}(?=@{})'.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS,
'{,200}', HOSTNAME),
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
result = re.sub(r'(?<=[^\w\.\-\+]){}{}{}(?=@{})'
.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS, '{,4}',
HOSTNAME),
'[FILTERED]', self.text)
return re.sub(r'(?<={}{}{}){}{}(?=@{})'
.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS, '{2}',
EMAIL_ADDRESS_SYMBOLS, '{,197}',
HOSTNAME),
'[FILTERED]', result)
def _filter_entire_email(self):
return re.sub(r'{}'.format(EMAIL_ADDRESS), '[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
for times in range(2, -1, -1):
result = re.sub('{}{}{}{}{}{}'
.format(r'((?<=00[1-9]\d{', times,
'})|(?<=\+[1-9]\d{', times,
'}))', PHONE_NUMBER),
' [FILTERED]', result)
result = self._filter_phone_number(result, '[FILTERED]')
return result
def _filter_phone_number(self, result, filtered='[PHONE]'):
return re.sub(r'{}'.format(PHONE_FULL), filtered, result)
def _filter_email(self):
if self.preserve_email_hostname:
return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
return self._partially_preserve_email_username()
else:
return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
return self._preserve_phone_country_code(result)
else:
return self._filter_phone_number(result)
def filtered(self):
return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
return re.match(r'^{}$'.format(EMAIL_ADDRESS), value)
@classmethod
def is_phone(self, value):
return re.match(r'^{}$'.format(PHONE_FULL), value)
@classmethod
def is_hostname(self, value):
return re.match(r'^{}$'.format(HOSTNAME), value)
@classmethod
def is_ip_address(self, value):
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'.format(ip=IP_RANGE), value)
@classmethod
def is_number(self, value):
return re.match(r'^{}\.?\d*$'.format(INTEGER), value)
@classmethod
def is_integer(self, value):
return re.match(r'^{}$'.format(INTEGER), value)
@classmethod
def is_date(self, value):
return re.match(r'^{}$'.format(DATE), value)
@classmethod
def is_time(self, value):
return re.match(r'^{}$'.format(TIME), value)
@classmethod
def is_datetime(self, value):
return re.match(r'^{}(\s|T){}$'.format(DATE, TIME), value)

Драгомир обнови решението на 22.04.2014 18:25 (преди около 10 години)

import re
HOSTNAME = '([a-zA-Z0-9]([a-zA-Z0-9\-]{,61}[a-zA-Z0-9])?\.)*'\
'[a-zA-Z]{2,3}(\.[a-zA-Z]{2})?'
EMAIL_ADDRESS_SYMBOLS = '(\w|[\.\-\+])'
EMAIL_ADDRESS = '[a-zA-Z0-9]{}{}@{}'\
.format(EMAIL_ADDRESS_SYMBOLS, '{,200}', HOSTNAME)
PHONE_CODE = '(0|((00|\+)[1-9](\d{2}|\d)?))'
PHONE_SEPARATORS = '[\s\-\(\)]{,2}'
PHONE_NUMBER = '({}\d){}({})?\d'\
.format(PHONE_SEPARATORS, '{5,10}', PHONE_SEPARATORS)
PHONE_FULL = '{}{}'.format(PHONE_CODE, PHONE_NUMBER)
IP_RANGE = '(\d|[1-9]\d|1[1-9][1-9]|2[0-4]\d|25[0-5])'
-INTEGER = '-?0?([1-9]\d*)?'
+INTEGER = '-?(0|([1-9]\d*))'
DATE = '\d{4}\-([1-9]|1[0-2])\-([1-9]|[1-2]\d|3[0-1])'
TIME = '(0\d|1[\d|2[0-3]):[0-5]\d:[0-5]\d'
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 _preserve_email_hostname(self):
return re.sub(r'{}{}{}(?=@{})'.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS,
'{,200}', HOSTNAME),
'[FILTERED]', self.text)
def _partially_preserve_email_username(self):
result = re.sub(r'(?<=[^\w\.\-\+]){}{}{}(?=@{})'
.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS, '{,4}',
HOSTNAME),
'[FILTERED]', self.text)
return re.sub(r'(?<={}{}{}){}{}(?=@{})'
.format('[a-zA-Z0-9]',
EMAIL_ADDRESS_SYMBOLS, '{2}',
EMAIL_ADDRESS_SYMBOLS, '{,197}',
HOSTNAME),
'[FILTERED]', result)
def _filter_entire_email(self):
return re.sub(r'{}'.format(EMAIL_ADDRESS), '[EMAIL]', self.text)
def _preserve_phone_country_code(self, result):
for times in range(2, -1, -1):
result = re.sub('{}{}{}{}{}{}'
.format(r'((?<=00[1-9]\d{', times,
'})|(?<=\+[1-9]\d{', times,
'}))', PHONE_NUMBER),
' [FILTERED]', result)
result = self._filter_phone_number(result, '[FILTERED]')
return result
def _filter_phone_number(self, result, filtered='[PHONE]'):
return re.sub(r'{}'.format(PHONE_FULL), filtered, result)
def _filter_email(self):
if self.preserve_email_hostname:
return self._preserve_email_hostname()
elif self.partially_preserve_email_username:
return self._partially_preserve_email_username()
else:
return self._filter_entire_email()
def _filter_phone(self, result):
if self.preserve_phone_country_code:
return self._preserve_phone_country_code(result)
else:
return self._filter_phone_number(result)
def filtered(self):
return self._filter_phone(self._filter_email())
class Validations():
@classmethod
def is_email(self, value):
return re.match(r'^{}$'.format(EMAIL_ADDRESS), value)
@classmethod
def is_phone(self, value):
return re.match(r'^{}$'.format(PHONE_FULL), value)
@classmethod
def is_hostname(self, value):
return re.match(r'^{}$'.format(HOSTNAME), value)
@classmethod
def is_ip_address(self, value):
return re.match(r'^{ip}\.{ip}\.{ip}\.{ip}$'.format(ip=IP_RANGE), value)
@classmethod
def is_number(self, value):
return re.match(r'^{}\.?\d*$'.format(INTEGER), value)
@classmethod
def is_integer(self, value):
return re.match(r'^{}$'.format(INTEGER), value)
@classmethod
def is_date(self, value):
return re.match(r'^{}$'.format(DATE), value)
@classmethod
def is_time(self, value):
return re.match(r'^{}$'.format(TIME), value)
@classmethod
def is_datetime(self, value):
return re.match(r'^{}(\s|T){}$'.format(DATE, TIME), value)