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

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

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

Резултати

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

Код

import re
class PrivacyFilter():
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
self.phone_pattern = Validations.phone_pattern[:-1] + r'\b'
self.email_pattern = r'{}{}{}'\
.format(r'\b', Validations.email_pattern[:-1], r'\b')
def filter_email(self, filtered_text):
# Email Filter
while re.search(self.email_pattern, filtered_text):
if (self.preserve_email_hostname or
self.partially_preserve_email_username):
email_match_obj = re.search(self.email_pattern, filtered_text)
if email_match_obj is not None:
email_hostname = email_match_obj.group('hostname')
prefix_to_add = ''
if (self.partially_preserve_email_username and
len(email_match_obj.group('prefix')) >= 6):
prefix_to_add = email_match_obj.group('prefix')[:3]
filtered_text = re.sub(self.email_pattern,
prefix_to_add +
'[FILTERED]@' +
email_hostname,
filtered_text, count=1)
else:
filtered_text = re.sub(self.email_pattern,
'[EMAIL]',
filtered_text)
return filtered_text
def filter_phone(self, filtered_text):
# Phone Filter
while re.search(self.phone_pattern, filtered_text):
if self.preserve_phone_country_code:
phone_match_obj = re.search(self.phone_pattern, filtered_text)
if phone_match_obj is not None:
if phone_match_obj.group('code') is not None:
filtered_text = re.sub(self.phone_pattern,
phone_match_obj.group('code') +
' [FILTERED]',
filtered_text, count=1)
else:
filtered_text = re.sub(self.phone_pattern,
'[PHONE]',
filtered_text, count=1)
else:
filtered_text = re.sub(self.phone_pattern,
'[PHONE]',
filtered_text)
return filtered_text
def filtered(self):
filtered_text = self.text
filtered_text = self.filter_email(filtered_text)
filtered_text = self.filter_phone(filtered_text)
return filtered_text
class Validations():
hostname_pattern = \
(r'(([a-zA-Z0-9]{,63}|[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*)$')
email_pattern = \
(r'(?P<prefix>[a-zA-Z0-9][\w\+\.\-]{,200})@'
r'(?P<hostname>(([a-zA-Z0-9]{,63}|'
r'[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*))$')
phone_pattern = \
(r'(0[1-9]([\ \-\(\)]{,2}[0-9]){5,10}|'
r'(?P<code>(\b00|\+)[1-9][0-9]{,2})'
r'([\ \-\(\)]{,2}[0-9]){6,11})$')
ip_address_pattern = \
(r'((1*[0-9]{1,2}|2[0-5]{2})\.){3}(1*[0-9]{1,2}|2[0-5]{2})$')
number_pattern = \
(r'\-*(0|[1-9][0-9]*)(\.[0-9]+)*$')
integer_pattern = \
(r'\-*(0|[1-9][0-9]*)$')
date_pattern = \
(r'[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])$')
time_pattern = \
(r'([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
datetime_pattern = \
(r'[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])[\ T]'
r'([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
def __init__():
pass
@classmethod
def is_hostname(cls, value):
return bool(re.match(cls.hostname_pattern, value))
@classmethod
def is_email(cls, value):
return bool(re.match(cls.email_pattern, value))
@classmethod
def is_phone(cls, value):
return bool(re.match(cls.phone_pattern, value))
@classmethod
def is_ip_address(cls, value):
return bool(re.match(cls.ip_address_pattern, value))
@classmethod
def is_number(cls, value):
return bool(re.match(cls.number_pattern, value))
@classmethod
def is_integer(cls, value):
return bool(re.match(cls.integer_pattern, value))
@classmethod
def is_date(cls, value):
return bool(re.match(cls.date_pattern, value))
@classmethod
def is_time(cls, value):
return bool(re.match(cls.time_pattern, value))
@classmethod
def is_datetime(cls, value):
return bool(re.match(cls.datetime_pattern, value))

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

....F...........F.....FF..FF......F....
======================================================================
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-nlzdb/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Reach me at: 0885123' != 'Reach me at: [PHONE]'
- Reach me at: 0885123
+ Reach me at: [PHONE]


======================================================================
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-nlzdb/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_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-nlzdb/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-nlzdb/test.py", line 179, in test_handles_multiline_strings_in_hostname_validation_properly
    self.assertFalse(solution.Validations.is_hostname("foo.com\n"))
AssertionError: True is not false

======================================================================
FAIL: test_handles_newlines_in_date_validation (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-nlzdb/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-nlzdb/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_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-nlzdb/test.py", line 229, in test_validates_more_complex_integers
    self.assertFalse(solution.Validations.is_integer('--2132'))
AssertionError: True is not false

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

FAILED (failures=7)

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

Теодор обнови решението на 19.04.2014 04:23 (преди над 10 години)

+import re
+
+
+class PrivacyFilter():
+
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, text):
+ self.text = text
+ self.phone_pattern = Validations.phone_pattern[1:-1]
+ self.email_pattern = Validations.email_pattern[1:-1]
+
+ def filtered(self):
+ filtered_text = self.text
+
+ # Phone Filter
+ if self.preserve_phone_country_code:
+ phone_match_obj = re.search(self.phone_pattern, filtered_text)
+ country_code = phone_match_obj.group('code')
+ if country_code != '0':
+ filtered_text = re.sub(self.phone_pattern,
+ country_code+' [FILTERED]',
+ filtered_text)
+ else:
+ filtered_text = re.sub(self.phone_pattern,
+ '[PHONE]',
+ filtered_text)
+ else:
+ filtered_text = re.sub(self.phone_pattern,
+ '[PHONE]',
+ filtered_text)
+
+ if self.partially_preserve_email_username:
+ self.preserve_email_hostname = True
+
+ # Email Filter
+ if self.preserve_email_hostname:
+ email_match_obj = re.search(self.email_pattern, filtered_text)
+ email_hostname = email_match_obj.group('hostname')
+ prefix_to_add = ''
+ if self.partially_preserve_email_username and \
+ len(email_match_obj.group('prefix')) >= 6:
+ prefix_to_add = email_match_obj.group('prefix')[:3]
+
+ filtered_text = re.sub(self.email_pattern,
+ prefix_to_add+'[FILTERED]@'+email_hostname,
+ filtered_text)
+ else:
+ filtered_text = re.sub(self.email_pattern,
+ '[EMAIL]',
+ filtered_text)
+
+ return filtered_text
+
+
+class Validations():
+ hostname_pattern = \
+ (r'^(([a-zA-Z0-9]{,63}|[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
+ r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*)$')
+
+ email_pattern = \
+ (r'^(?P<prefix>[a-zA-Z0-9][\w\+\.\-]{,200})@'
+ r'(?P<hostname>(([a-zA-Z0-9]{,63}|'
+ r'[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
+ r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*))$')
+
+ phone_pattern = \
+ (r'^(?P<code>0|(00|\+)[1-9][0-9]{,2})'
+ r'([\ \-\(\)]{,2}[0-9]){6,11}$')
+
+ ip_address_pattern = \
+ (r'^((1*[0-9]{1,2}|2[0-5]{2})\.){3}(1*[0-9]{1,2}|2[0-5]{2})$')
+
+ number_pattern = \
+ (r'^\-*(0|[1-9][0-9]*)(\.[0-9]+)*$')
+
+ integer_pattern = \
+ (r'^\-*(0|[1-9][0-9]*)$')
+
+ date_pattern = \
+ (r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])$')
+
+ time_pattern = \
+ (r'^([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
+
+ datetime_pattern = \
+ (r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])[\ T]'
+ r'([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
+
+ def __init__():
+ pass
+
+ @classmethod
+ def is_hostname(cls, value):
+ return bool(re.search(cls.hostname_pattern, value))
+
+ @classmethod
+ def is_email(cls, value):
+ return bool(re.search(cls.email_pattern, value))
+
+ @classmethod
+ def is_phone(cls, value):
+ return bool(re.search(cls.phone_pattern, value))
+
+ @classmethod
+ def is_ip_address(cls, value):
+ return bool(re.search(cls.ip_address_pattern, value))
+
+ @classmethod
+ def is_number(cls, value):
+ return bool(re.search(cls.number_pattern, value))
+
+ @classmethod
+ def is_integer(cls, value):
+ return bool(re.search(cls.integer_pattern, value))
+
+ @classmethod
+ def is_date(cls, value):
+ return bool(re.search(cls.date_pattern, value))
+
+ @classmethod
+ def is_time(cls, value):
+ return bool(re.search(cls.time_pattern, value))
+
+ @classmethod
+ def is_datetime(cls, value):
+ return bool(re.search(cls.datetime_pattern, value))

Теодор обнови решението на 19.04.2014 13:18 (преди над 10 години)

import re
class PrivacyFilter():
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
self.phone_pattern = Validations.phone_pattern[1:-1]
self.email_pattern = Validations.email_pattern[1:-1]
def filtered(self):
filtered_text = self.text
# Phone Filter
if self.preserve_phone_country_code:
phone_match_obj = re.search(self.phone_pattern, filtered_text)
country_code = phone_match_obj.group('code')
if country_code != '0':
filtered_text = re.sub(self.phone_pattern,
country_code+' [FILTERED]',
filtered_text)
else:
filtered_text = re.sub(self.phone_pattern,
'[PHONE]',
filtered_text)
else:
filtered_text = re.sub(self.phone_pattern,
'[PHONE]',
filtered_text)
- if self.partially_preserve_email_username:
- self.preserve_email_hostname = True
-
# Email Filter
- if self.preserve_email_hostname:
+ if (self.preserve_email_hostname or
+ self.partially_preserve_email_username):
+
email_match_obj = re.search(self.email_pattern, filtered_text)
email_hostname = email_match_obj.group('hostname')
prefix_to_add = ''
- if self.partially_preserve_email_username and \
- len(email_match_obj.group('prefix')) >= 6:
+ if (self.partially_preserve_email_username and
+ len(email_match_obj.group('prefix')) >= 6):
prefix_to_add = email_match_obj.group('prefix')[:3]
filtered_text = re.sub(self.email_pattern,
prefix_to_add+'[FILTERED]@'+email_hostname,
filtered_text)
else:
filtered_text = re.sub(self.email_pattern,
'[EMAIL]',
filtered_text)
return filtered_text
class Validations():
hostname_pattern = \
(r'^(([a-zA-Z0-9]{,63}|[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*)$')
email_pattern = \
(r'^(?P<prefix>[a-zA-Z0-9][\w\+\.\-]{,200})@'
r'(?P<hostname>(([a-zA-Z0-9]{,63}|'
r'[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*))$')
phone_pattern = \
(r'^(?P<code>0|(00|\+)[1-9][0-9]{,2})'
r'([\ \-\(\)]{,2}[0-9]){6,11}$')
ip_address_pattern = \
(r'^((1*[0-9]{1,2}|2[0-5]{2})\.){3}(1*[0-9]{1,2}|2[0-5]{2})$')
number_pattern = \
(r'^\-*(0|[1-9][0-9]*)(\.[0-9]+)*$')
integer_pattern = \
(r'^\-*(0|[1-9][0-9]*)$')
date_pattern = \
(r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])$')
time_pattern = \
(r'^([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
datetime_pattern = \
(r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])[\ T]'
r'([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
def __init__():
pass
@classmethod
def is_hostname(cls, value):
return bool(re.search(cls.hostname_pattern, value))
@classmethod
def is_email(cls, value):
return bool(re.search(cls.email_pattern, value))
@classmethod
def is_phone(cls, value):
return bool(re.search(cls.phone_pattern, value))
@classmethod
def is_ip_address(cls, value):
return bool(re.search(cls.ip_address_pattern, value))
@classmethod
def is_number(cls, value):
return bool(re.search(cls.number_pattern, value))
@classmethod
def is_integer(cls, value):
return bool(re.search(cls.integer_pattern, value))
@classmethod
def is_date(cls, value):
return bool(re.search(cls.date_pattern, value))
@classmethod
def is_time(cls, value):
return bool(re.search(cls.time_pattern, value))
@classmethod
def is_datetime(cls, value):
return bool(re.search(cls.datetime_pattern, value))

Теодор обнови решението на 21.04.2014 20:44 (преди над 10 години)

import re
class PrivacyFilter():
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
self.phone_pattern = Validations.phone_pattern[1:-1]
self.email_pattern = Validations.email_pattern[1:-1]
def filtered(self):
filtered_text = self.text
# Phone Filter
if self.preserve_phone_country_code:
phone_match_obj = re.search(self.phone_pattern, filtered_text)
- country_code = phone_match_obj.group('code')
- if country_code != '0':
- filtered_text = re.sub(self.phone_pattern,
- country_code+' [FILTERED]',
- filtered_text)
- else:
- filtered_text = re.sub(self.phone_pattern,
- '[PHONE]',
- filtered_text)
+ if phone_match_obj is not None:
+ country_code = phone_match_obj.group('code')
+ if country_code != '0':
+ filtered_text = re.sub(self.phone_pattern,
+ country_code+' [FILTERED]',
+ filtered_text)
+ else:
+ filtered_text = re.sub(self.phone_pattern,
+ '[PHONE]',
+ filtered_text)
else:
filtered_text = re.sub(self.phone_pattern,
'[PHONE]',
filtered_text)
# Email Filter
if (self.preserve_email_hostname or
self.partially_preserve_email_username):
-
+
email_match_obj = re.search(self.email_pattern, filtered_text)
- email_hostname = email_match_obj.group('hostname')
- prefix_to_add = ''
- if (self.partially_preserve_email_username and
- len(email_match_obj.group('prefix')) >= 6):
- prefix_to_add = email_match_obj.group('prefix')[:3]
+ if email_match_obj is not None:
+ email_hostname = email_match_obj.group('hostname')
+ prefix_to_add = ''
+ if (self.partially_preserve_email_username and
+ len(email_match_obj.group('prefix')) >= 6):
+ prefix_to_add = email_match_obj.group('prefix')[:3]
- filtered_text = re.sub(self.email_pattern,
- prefix_to_add+'[FILTERED]@'+email_hostname,
- filtered_text)
+ filtered_text = re.sub(self.email_pattern,
+ prefix_to_add +
+ '[FILTERED]@' +
+ email_hostname,
+ filtered_text)
else:
filtered_text = re.sub(self.email_pattern,
'[EMAIL]',
filtered_text)
return filtered_text
class Validations():
hostname_pattern = \
(r'^(([a-zA-Z0-9]{,63}|[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*)$')
email_pattern = \
(r'^(?P<prefix>[a-zA-Z0-9][\w\+\.\-]{,200})@'
r'(?P<hostname>(([a-zA-Z0-9]{,63}|'
r'[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*))$')
phone_pattern = \
(r'^(?P<code>0|(00|\+)[1-9][0-9]{,2})'
r'([\ \-\(\)]{,2}[0-9]){6,11}$')
ip_address_pattern = \
(r'^((1*[0-9]{1,2}|2[0-5]{2})\.){3}(1*[0-9]{1,2}|2[0-5]{2})$')
number_pattern = \
(r'^\-*(0|[1-9][0-9]*)(\.[0-9]+)*$')
integer_pattern = \
(r'^\-*(0|[1-9][0-9]*)$')
date_pattern = \
(r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])$')
time_pattern = \
(r'^([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
datetime_pattern = \
(r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])[\ T]'
r'([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
def __init__():
pass
@classmethod
def is_hostname(cls, value):
return bool(re.search(cls.hostname_pattern, value))
@classmethod
def is_email(cls, value):
return bool(re.search(cls.email_pattern, value))
@classmethod
def is_phone(cls, value):
return bool(re.search(cls.phone_pattern, value))
@classmethod
def is_ip_address(cls, value):
return bool(re.search(cls.ip_address_pattern, value))
@classmethod
def is_number(cls, value):
return bool(re.search(cls.number_pattern, value))
@classmethod
def is_integer(cls, value):
return bool(re.search(cls.integer_pattern, value))
@classmethod
def is_date(cls, value):
return bool(re.search(cls.date_pattern, value))
@classmethod
def is_time(cls, value):
return bool(re.search(cls.time_pattern, value))
@classmethod
def is_datetime(cls, value):
return bool(re.search(cls.datetime_pattern, value))

Теодор обнови решението на 22.04.2014 15:29 (преди над 10 години)

import re
class PrivacyFilter():
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
- self.phone_pattern = Validations.phone_pattern[1:-1]
- self.email_pattern = Validations.email_pattern[1:-1]
-
+ self.phone_pattern = '{}{}{}'.format(r'\b',
+ Validations.phone_pattern[1:-1],
+ r'\b')
+ self.email_pattern = '{}{}{}'.format(r'\b',
+ Validations.email_pattern[1:-1],
+ r'\b')
+
def filtered(self):
filtered_text = self.text
- # Phone Filter
- if self.preserve_phone_country_code:
- phone_match_obj = re.search(self.phone_pattern, filtered_text)
- if phone_match_obj is not None:
- country_code = phone_match_obj.group('code')
- if country_code != '0':
- filtered_text = re.sub(self.phone_pattern,
- country_code+' [FILTERED]',
- filtered_text)
- else:
- filtered_text = re.sub(self.phone_pattern,
- '[PHONE]',
- filtered_text)
- else:
- filtered_text = re.sub(self.phone_pattern,
- '[PHONE]',
- filtered_text)
-
# Email Filter
if (self.preserve_email_hostname or
self.partially_preserve_email_username):
email_match_obj = re.search(self.email_pattern, filtered_text)
if email_match_obj is not None:
email_hostname = email_match_obj.group('hostname')
prefix_to_add = ''
if (self.partially_preserve_email_username and
len(email_match_obj.group('prefix')) >= 6):
prefix_to_add = email_match_obj.group('prefix')[:3]
filtered_text = re.sub(self.email_pattern,
prefix_to_add +
'[FILTERED]@' +
email_hostname,
filtered_text)
else:
filtered_text = re.sub(self.email_pattern,
'[EMAIL]',
filtered_text)
+ # Phone Filter
+ if self.preserve_phone_country_code:
+ phone_match_obj = re.search(self.phone_pattern, filtered_text)
+ if phone_match_obj is not None:
+ if phone_match_obj.group('çode') is not None:
+ filtered_text = re.sub(self.phone_pattern,
+ phone_match_obj.group('code')+
+ ' [FILTERED]',
+ filtered_text)
+ else:
+ filtered_text = re.sub(self.phone_pattern,
+ '[PHONE]',
+ filtered_text)
+ else:
+ filtered_text = re.sub(self.phone_pattern,
+ '[PHONE]',
+ filtered_text)
+
return filtered_text
class Validations():
hostname_pattern = \
(r'^(([a-zA-Z0-9]{,63}|[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*)$')
email_pattern = \
(r'^(?P<prefix>[a-zA-Z0-9][\w\+\.\-]{,200})@'
r'(?P<hostname>(([a-zA-Z0-9]{,63}|'
r'[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*))$')
phone_pattern = \
- (r'^(?P<code>0|(00|\+)[1-9][0-9]{,2})'
- r'([\ \-\(\)]{,2}[0-9]){6,11}$')
+ (r'^(0[1-9]([\ \-\(\)]{,2}[0-9]){5,10}|'
+ r'(?P<code>00|\+)[1-9][0-9]{,2}'
+ r'([\ \-\(\)]{,2}[0-9]){6,11})$')
ip_address_pattern = \
(r'^((1*[0-9]{1,2}|2[0-5]{2})\.){3}(1*[0-9]{1,2}|2[0-5]{2})$')
number_pattern = \
(r'^\-*(0|[1-9][0-9]*)(\.[0-9]+)*$')
integer_pattern = \
(r'^\-*(0|[1-9][0-9]*)$')
date_pattern = \
(r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])$')
time_pattern = \
(r'^([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
datetime_pattern = \
(r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])[\ T]'
r'([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
def __init__():
pass
@classmethod
def is_hostname(cls, value):
return bool(re.search(cls.hostname_pattern, value))
@classmethod
def is_email(cls, value):
return bool(re.search(cls.email_pattern, value))
@classmethod
def is_phone(cls, value):
return bool(re.search(cls.phone_pattern, value))
@classmethod
def is_ip_address(cls, value):
return bool(re.search(cls.ip_address_pattern, value))
@classmethod
def is_number(cls, value):
return bool(re.search(cls.number_pattern, value))
@classmethod
def is_integer(cls, value):
return bool(re.search(cls.integer_pattern, value))
@classmethod
def is_date(cls, value):
return bool(re.search(cls.date_pattern, value))
@classmethod
def is_time(cls, value):
return bool(re.search(cls.time_pattern, value))
@classmethod
def is_datetime(cls, value):
return bool(re.search(cls.datetime_pattern, value))

Теодор обнови решението на 23.04.2014 00:05 (преди над 10 години)

import re
class PrivacyFilter():
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
- self.phone_pattern = '{}{}{}'.format(r'\b',
- Validations.phone_pattern[1:-1],
- r'\b')
- self.email_pattern = '{}{}{}'.format(r'\b',
- Validations.email_pattern[1:-1],
- r'\b')
-
+ self.phone_pattern = Validations.phone_pattern[1:-1]
+ self.email_pattern = '{}{}{}'\
+ .format(r'\b', Validations.email_pattern[1:-1], r'\b')
+
def filtered(self):
filtered_text = self.text
# Email Filter
if (self.preserve_email_hostname or
self.partially_preserve_email_username):
email_match_obj = re.search(self.email_pattern, filtered_text)
if email_match_obj is not None:
email_hostname = email_match_obj.group('hostname')
prefix_to_add = ''
if (self.partially_preserve_email_username and
len(email_match_obj.group('prefix')) >= 6):
prefix_to_add = email_match_obj.group('prefix')[:3]
filtered_text = re.sub(self.email_pattern,
prefix_to_add +
'[FILTERED]@' +
email_hostname,
filtered_text)
else:
filtered_text = re.sub(self.email_pattern,
'[EMAIL]',
filtered_text)
# Phone Filter
if self.preserve_phone_country_code:
phone_match_obj = re.search(self.phone_pattern, filtered_text)
if phone_match_obj is not None:
- if phone_match_obj.group('çode') is not None:
+ if phone_match_obj.group('code') is not None:
filtered_text = re.sub(self.phone_pattern,
- phone_match_obj.group('code')+
+ phone_match_obj.group('code') +
' [FILTERED]',
filtered_text)
else:
filtered_text = re.sub(self.phone_pattern,
'[PHONE]',
filtered_text)
else:
filtered_text = re.sub(self.phone_pattern,
'[PHONE]',
filtered_text)
return filtered_text
class Validations():
hostname_pattern = \
(r'^(([a-zA-Z0-9]{,63}|[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*)$')
email_pattern = \
(r'^(?P<prefix>[a-zA-Z0-9][\w\+\.\-]{,200})@'
r'(?P<hostname>(([a-zA-Z0-9]{,63}|'
r'[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*))$')
phone_pattern = \
(r'^(0[1-9]([\ \-\(\)]{,2}[0-9]){5,10}|'
- r'(?P<code>00|\+)[1-9][0-9]{,2}'
+ r'(?P<code>(00|\+)[1-9][0-9]{,2})'
r'([\ \-\(\)]{,2}[0-9]){6,11})$')
ip_address_pattern = \
(r'^((1*[0-9]{1,2}|2[0-5]{2})\.){3}(1*[0-9]{1,2}|2[0-5]{2})$')
number_pattern = \
(r'^\-*(0|[1-9][0-9]*)(\.[0-9]+)*$')
integer_pattern = \
(r'^\-*(0|[1-9][0-9]*)$')
date_pattern = \
(r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])$')
time_pattern = \
(r'^([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
datetime_pattern = \
(r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])[\ T]'
r'([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
def __init__():
pass
@classmethod
def is_hostname(cls, value):
return bool(re.search(cls.hostname_pattern, value))
@classmethod
def is_email(cls, value):
return bool(re.search(cls.email_pattern, value))
@classmethod
def is_phone(cls, value):
return bool(re.search(cls.phone_pattern, value))
@classmethod
def is_ip_address(cls, value):
return bool(re.search(cls.ip_address_pattern, value))
@classmethod
def is_number(cls, value):
return bool(re.search(cls.number_pattern, value))
@classmethod
def is_integer(cls, value):
return bool(re.search(cls.integer_pattern, value))
@classmethod
def is_date(cls, value):
return bool(re.search(cls.date_pattern, value))
@classmethod
def is_time(cls, value):
return bool(re.search(cls.time_pattern, value))
@classmethod
def is_datetime(cls, value):
return bool(re.search(cls.datetime_pattern, value))

Теодор обнови решението на 23.04.2014 13:52 (преди над 10 години)

import re
class PrivacyFilter():
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
- self.phone_pattern = Validations.phone_pattern[1:-1]
- self.email_pattern = '{}{}{}'\
- .format(r'\b', Validations.email_pattern[1:-1], r'\b')
+ self.phone_pattern = Validations.phone_pattern[:-1] + r'\b'
+ self.email_pattern = r'{}{}{}'\
+ .format(r'\b', Validations.email_pattern[:-1], r'\b')
- def filtered(self):
- filtered_text = self.text
-
+ def filter_email(self, filtered_text):
# Email Filter
- if (self.preserve_email_hostname or
- self.partially_preserve_email_username):
+ while re.search(self.email_pattern, filtered_text):
- email_match_obj = re.search(self.email_pattern, filtered_text)
- if email_match_obj is not None:
- email_hostname = email_match_obj.group('hostname')
- prefix_to_add = ''
- if (self.partially_preserve_email_username and
- len(email_match_obj.group('prefix')) >= 6):
- prefix_to_add = email_match_obj.group('prefix')[:3]
+ if (self.preserve_email_hostname or
+ self.partially_preserve_email_username):
+ email_match_obj = re.search(self.email_pattern, filtered_text)
+ if email_match_obj is not None:
+ email_hostname = email_match_obj.group('hostname')
+ prefix_to_add = ''
+ if (self.partially_preserve_email_username and
+ len(email_match_obj.group('prefix')) >= 6):
+ prefix_to_add = email_match_obj.group('prefix')[:3]
+
+ filtered_text = re.sub(self.email_pattern,
+ prefix_to_add +
+ '[FILTERED]@' +
+ email_hostname,
+ filtered_text, count=1)
+ else:
filtered_text = re.sub(self.email_pattern,
- prefix_to_add +
- '[FILTERED]@' +
- email_hostname,
+ '[EMAIL]',
filtered_text)
- else:
- filtered_text = re.sub(self.email_pattern,
- '[EMAIL]',
- filtered_text)
+ return filtered_text
+
+ def filter_phone(self, filtered_text):
# Phone Filter
- if self.preserve_phone_country_code:
- phone_match_obj = re.search(self.phone_pattern, filtered_text)
- if phone_match_obj is not None:
- if phone_match_obj.group('code') is not None:
- filtered_text = re.sub(self.phone_pattern,
- phone_match_obj.group('code') +
- ' [FILTERED]',
- filtered_text)
- else:
- filtered_text = re.sub(self.phone_pattern,
- '[PHONE]',
- filtered_text)
- else:
- filtered_text = re.sub(self.phone_pattern,
- '[PHONE]',
- filtered_text)
+ while re.search(self.phone_pattern, filtered_text):
+ if self.preserve_phone_country_code:
+ phone_match_obj = re.search(self.phone_pattern, filtered_text)
+ if phone_match_obj is not None:
+ if phone_match_obj.group('code') is not None:
+ filtered_text = re.sub(self.phone_pattern,
+ phone_match_obj.group('code') +
+ ' [FILTERED]',
+ filtered_text, count=1)
+ else:
+ filtered_text = re.sub(self.phone_pattern,
+ '[PHONE]',
+ filtered_text, count=1)
+ else:
+ filtered_text = re.sub(self.phone_pattern,
+ '[PHONE]',
+ filtered_text)
+
return filtered_text
+ def filtered(self):
+ filtered_text = self.text
+ filtered_text = self.filter_email(filtered_text)
+ filtered_text = self.filter_phone(filtered_text)
+ return filtered_text
+
class Validations():
hostname_pattern = \
- (r'^(([a-zA-Z0-9]{,63}|[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
+ (r'(([a-zA-Z0-9]{,63}|[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*)$')
email_pattern = \
- (r'^(?P<prefix>[a-zA-Z0-9][\w\+\.\-]{,200})@'
+ (r'(?P<prefix>[a-zA-Z0-9][\w\+\.\-]{,200})@'
r'(?P<hostname>(([a-zA-Z0-9]{,63}|'
r'[a-zA-Z0-9][a-zA-Z0-9\-]{,61}[a-zA-Z0-9])\.)+'
r'([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})*))$')
phone_pattern = \
- (r'^(0[1-9]([\ \-\(\)]{,2}[0-9]){5,10}|'
- r'(?P<code>(00|\+)[1-9][0-9]{,2})'
+ (r'(0[1-9]([\ \-\(\)]{,2}[0-9]){5,10}|'
+ r'(?P<code>(\b00|\+)[1-9][0-9]{,2})'
r'([\ \-\(\)]{,2}[0-9]){6,11})$')
ip_address_pattern = \
- (r'^((1*[0-9]{1,2}|2[0-5]{2})\.){3}(1*[0-9]{1,2}|2[0-5]{2})$')
+ (r'((1*[0-9]{1,2}|2[0-5]{2})\.){3}(1*[0-9]{1,2}|2[0-5]{2})$')
number_pattern = \
- (r'^\-*(0|[1-9][0-9]*)(\.[0-9]+)*$')
+ (r'\-*(0|[1-9][0-9]*)(\.[0-9]+)*$')
integer_pattern = \
- (r'^\-*(0|[1-9][0-9]*)$')
+ (r'\-*(0|[1-9][0-9]*)$')
date_pattern = \
- (r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])$')
+ (r'[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])$')
time_pattern = \
- (r'^([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
+ (r'([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
datetime_pattern = \
- (r'^[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])[\ T]'
+ (r'[0-9]{4}\-(0[1-9]|1[0-2])\-([0-2][1-9]|3[0-1])[\ T]'
r'([01][0-9]|2[0-3])(\:[0-5][0-9]){2}$')
def __init__():
pass
@classmethod
def is_hostname(cls, value):
- return bool(re.search(cls.hostname_pattern, value))
+ return bool(re.match(cls.hostname_pattern, value))
@classmethod
def is_email(cls, value):
- return bool(re.search(cls.email_pattern, value))
+ return bool(re.match(cls.email_pattern, value))
@classmethod
def is_phone(cls, value):
- return bool(re.search(cls.phone_pattern, value))
+ return bool(re.match(cls.phone_pattern, value))
@classmethod
def is_ip_address(cls, value):
- return bool(re.search(cls.ip_address_pattern, value))
+ return bool(re.match(cls.ip_address_pattern, value))
@classmethod
def is_number(cls, value):
- return bool(re.search(cls.number_pattern, value))
+ return bool(re.match(cls.number_pattern, value))
@classmethod
def is_integer(cls, value):
- return bool(re.search(cls.integer_pattern, value))
+ return bool(re.match(cls.integer_pattern, value))
@classmethod
def is_date(cls, value):
- return bool(re.search(cls.date_pattern, value))
+ return bool(re.match(cls.date_pattern, value))
@classmethod
def is_time(cls, value):
- return bool(re.search(cls.time_pattern, value))
+ return bool(re.match(cls.time_pattern, value))
@classmethod
def is_datetime(cls, value):
- return bool(re.search(cls.datetime_pattern, value))
+ return bool(re.match(cls.datetime_pattern, value))