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

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

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

Резултати

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

Код

import re
def check(regex, text):
if re.search(regex, text, re.VERBOSE):
return True
else:
return False
class Validations(object):
def is_email(text):
regex_email = r'''^(?:[\w][\+\-\_\.\w]*)@
(?:[\w][a-zA-Z\-]{0,61}[^W-]\.)+
(?:(?:[a-zA-Z]{2}\.)(?:[a-zA-Z]{2}$)|(?:[a-zA-Z]{2,3}$))'''
return check(regex_email, text)
def is_hostname(text):
regex_host = r'''^(?:[\w][0-9a-zA-Z\-]{0,62}?(?<!\-)\.)+
(?:(?:[a-zA-Z]{2}$)|(?:[a-zA-Z]{2}\.[a-zA-Z]{2}$)|
(?:[a-zA-Z]{3}$)|([a-zA-Z]{3}\.[a-zA-Z]{2,3}$))'''
return check(regex_host, text)
def is_ip_address(text):
regex_ip = r'''^((([01]?[0-9]?[0-9]|[2]?[05]?[05])(\.)){3}
([01]?[0-9]?[0-9]|[2]?[05]?[05]))$'''
return check(regex_ip, text)
def is_number(text):
regex = r'^[-]?((?:[1-9][0-9]*\.?[0-9]+)$|(?:[0-9](?:$|\.[0-9]+)))'
return check(regex, text)
def is_integer(text):
regex_integer = r'^(?:[-]?[1-9][0-9]*)$|^(?:[-]?[0])$'
return check(regex_integer, text)
def is_date(text):
regex_date = r'''^(?:[0-9]{4}-(?:[0][1-9]|[1][0-2])-(?:[0][1-9]|
[1-2][0-9]|[3][01]))$'''
return check(regex_date, text)
def is_time(text):
regex_time = r'''^(?:([01][0-9]|[2][0-3]):([0-4][0-9]|[5][0-9]):
([0-4][0-9]|[5][0-9]))$'''
return check(regex_time, text)
def is_datetime(text):
date_time = re.split(r'[T| ]', text)
return Validations.is_date(date_time[0]) and \
Validations.is_time(date_time[1])
def is_phone(text):
regex_number = r'''^((?:(?:[0](?!0))|(?:(?:[0]{2}|\+)[1-9][0-9]{0,2})))
(?:[\ \-\(\)]{0,2}[0-9]){6,11}$'''
return check(regex_number, text)
class PrivacyFilter():
def __init__(self, text):
self.text = text
self.filtered_text = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.preserve_email_username = False
def filter_email(self, start, end):
all_regex = r'([^ ]+)@([^ ]+)'
self.filtered_text = self.text[:start]+"[EMAIL]"+self.text[end:]
def filter_username(self):
print("in")
left_regex = r'([^ ]+)@'
left_part = re.search(left_regex, self.text)
size_of_left_part = left_part.end() - left_part.start()-1
if size_of_left_part > 6:
result = self.text[:left_part.start()+3] + '[FILTERED]' + \
self.text[left_part.end()-1:]
else:
result = self.text[:left_part.start()] + '[FILTERED]' + \
self.text[left_part.end()-1:]
self.filtered_text = result
def filter_hostname(self):
left_regex = r'([^ ]+)@'
left_part = re.search(left_regex, self.text)
result = self.text[:left_part.start()] + '[FILTERED]' + \
self.text[left_part.end()-1:]
self.filtered_text = result
def filtered(self):
regex_mail = r'([^ ]+)@([^ ]+)'
position = re.search(regex_mail, self.text)
if position and Validations.is_email\
(self.text[position.start():position.end()]):
if self.preserve_email_username is False and \
self.preserve_email_hostname is False:
self.filter_email(position.start(), position.end())
elif self.preserve_email_username:
self.filter_username()
else:
self.filter_hostname()
regex_phonenumber = r'''((?:(?:[0](?!0))|(?:(?:[0]{2}|\+)
[1-9][0-9]{0,2})))(?:[\ \-\(\)]{0,2}[0-9]){6,11}'''
position_number = re.search(regex_phonenumber, self.text, re.VERBOSE)
if position_number:
if Validations.is_phone\
(self.text[position_number.start():position_number.end()]):
if self.preserve_phone_country_code is False:
self.filtered_text = \
self.filtered_text[:position_number.start()] + "[PHONE]" +\
self.filtered_text[position_number.end():]
else:
compile_ = re.compile(regex_phonenumber, re.VERBOSE)
country_code = compile_.search(self.text).groups()
size_of_code = len(country_code[0])
if size_of_code > 1:
self.filtered_text = \
self.filtered_text[:position_number.start() + \
size_of_code] + " [FILTERED]" + \
self.filtered_text[position_number.end():]
else:
self.filtered_text = \
self.filtered_text[:position_number.start()] +\
"[PHONE]" + self.filtered_text[position_number.end():]
return self.filtered_text

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

.FFFF.FF.......FF.....FF..FF.......F...
======================================================================
FAIL: test_allows_email_usernames_to_be_partially_preserved (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1ko05uj/test.py", line 58, in test_allows_email_usernames_to_be_partially_preserved
    self.assertEqual('som[FILTERED]@example.com', self.partially_filter_email_usernames('someone@example.com'))
AssertionError: 'som[FILTERED]@example.com' != '[EMAIL]'
- som[FILTERED]@example.com
+ [EMAIL]


======================================================================
FAIL: test_does_not_brake_with_unicode (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1ko05uj/test.py", line 64, in test_does_not_brake_with_unicode
    self.assertEqual('За връзка: [FILTERED]@example.com', self.partially_filter_email_usernames('За връзка: me@example.com'))
AssertionError: 'За връзка: [FILTERED]@example.com' != 'За връзка: [EMAIL]'
- За връзка: [FILTERED]@example.com
+ За връзка: [EMAIL]


======================================================================
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-1ko05uj/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-1ko05uj/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_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-1ko05uj/test.py", line 61, in test_filters_whole_email_usernames_if_too_short
    self.assertEqual('[FILTERED]@example.com', self.partially_filter_email_usernames('me@example.com'))
AssertionError: '[FILTERED]@example.com' != '[EMAIL]'
- [FILTERED]@example.com
+ [EMAIL]


======================================================================
FAIL: test_obfuscates_more_complicated_emails (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1ko05uj/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[EMAIL]' != 'larodi@x.com'
- [EMAIL]
+ larodi@x.com


======================================================================
FAIL: test_can_validate_more_complex_emails (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1ko05uj/test.py", line 124, in test_can_validate_more_complex_emails
    self.assertIs(solution.Validations.is_email(email), valid)
AssertionError: True is not False

======================================================================
FAIL: test_can_validate_more_complex_phone_numbers (test.ValidationsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 60, in thread
    raise it.exc_info[1]
  File "lib/language/python/runner.py", line 48, in run
    self.result = func(*args, **kwargs)
  File "/tmp/d20140513-11348-1ko05uj/test.py", line 160, in test_can_validate_more_complex_phone_numbers
    self.assertIs(solution.Validations.is_phone(phone), valid)
AssertionError: True is not False

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

----------------------------------------------------------------------
Ran 39 tests in 0.045s

FAILED (failures=13)

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

Пламен обнови решението на 23.04.2014 13:17 (преди над 10 години)

+import re
+
+
+def check(regex, text):
+ if re.search(regex, text, re.VERBOSE):
+ return True
+ else:
+ return False
+
+
+class Validations(object):
+
+ def is_email(text):
+ regex_email = r'''^(?:[\w][\+\-\_\.\w]*)@
+ (?:[\w][a-zA-Z\-]{0,61}[^W-]\.)+
+ (?:(?:[a-zA-Z]{2}\.)(?:[a-zA-Z]{2}$)|(?:[a-zA-Z]{2,3}$))'''
+ return check(regex_email, text)
+
+ def is_hostname(text):
+ regex_host = r'''^(?:[\w][0-9a-zA-Z\-]{0,62}?\.)+
+ (?:(?:[a-zA-Z]{2}$)|(?:[a-zA-Z]{2}\.[a-zA-Z]{2}$)|
+ (?:[a-zA-Z]{3}$)|([a-zA-Z]{3}\.[a-zA-Z]{2,3}$))'''
+ return check(regex_host, text)
+
+ def is_ip_address(text):
+ regex_ip = r'''^((([01]?[0-9]?[0-9]|[2]?[05]?[05])(\.)){3}
+ ([01]?[0-9]?[0-9]|[2]?[05]?[05]))$'''
+ return check(regex_ip, text)
+
+ def is_number(text):
+ regex = r'^[-]?((?:[1-9][0-9]*\.?[0-9]+)$|(?:[0-9](?:$|\.[0-9]+)))'
+ return check(regex, text)
+
+ def is_integer(text):
+ regex_integer = r'^(?:[-]?[1-9][0-9]*)$|^(?:[-]?[0])$'
+ return check(regex_integer, text)
+
+ def is_date(text):
+ regex_date = r'''^(?:[0-9]{4}-(?:[0][1-9]|[1][0-2])-(?:[0][1-9]|
+ [1-2][0-9]|[3][01]))$'''
+ return check(regex_date, text)
+
+ def is_time(text):
+ regex_time = r'''^(?:([01][0-9]|[2][0-3]):([0-4][0-9]|[5][0-9]):
+ ([0-4][0-9]|[5][0-9]))$'''
+ return check(regex_time, text)
+
+ def is_datetime(text):
+ date_time = re.split(r'[T| ]', text)
+ return Validations.is_date(date_time[0]) and \
+ Validations.is_time(date_time[1])
+
+ def is_phone(text):
+ regex_number = r'''^((?:(?:[0](?!0))|(?:(?:[0]{2}|\+)[1-9][0-9]{0,2})))
+ (?:[\ \-\(\)]{0,2}[0-9]){6,11}$'''
+ return check(regex_number, text)
+
+
+class PrivacyFilter():
+ def __init__(self, text):
+ self.text = text
+ self.filtered_text = text
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.preserve_email_username = False
+
+ def filter_email(self, start, end):
+ all_regex = r'([^ ]+)@([^ ]+)'
+ self.filtered_text = self.text[:start]+"[EMAIL]"+self.text[end:]
+
+ def filter_username(self):
+ print("in")
+ left_regex = r'([^ ]+)@'
+ left_part = re.search(left_regex, self.text)
+ size_of_left_part = left_part.end() - left_part.start()-1
+ if size_of_left_part > 6:
+ result = self.text[:left_part.start()+3] + '[FILTERED]' + \
+ self.text[left_part.end()-1:]
+ else:
+ result = self.text[:left_part.start()] + '[FILTERED]' + \
+ self.text[left_part.end()-1:]
+ self.filtered_text = result
+
+ def filter_hostname(self):
+ left_regex = r'([^ ]+)@'
+ left_part = re.search(left_regex, self.text)
+ result = self.text[:left_part.start()] + '[FILTERED]' + \
+ self.text[left_part.end()-1:]
+ self.filtered_text = result
+
+ def filtered(self):
+ regex_mail = r'([^ ]+)@([^ ]+)'
+ position = re.search(regex_mail, self.text)
+ if position and Validations.is_email\
+ (self.text[position.start():position.end()]):
+ if self.preserve_email_username is False and \
+ self.preserve_email_hostname is False:
+ self.filter_email(position.start(), position.end())
+ elif self.preserve_email_username:
+ self.filter_username()
+ else:
+ self.filter_hostname()
+
+ regex_phonenumber = r'''((?:(?:[0](?!0))|(?:(?:[0]{2}|\+)
+ [1-9][0-9]{0,2})))(?:[\ \-\(\)]{0,2}[0-9]){6,11}'''
+ position_number = re.search(regex_phonenumber, self.text, re.VERBOSE)
+ if position_number:
+ if Validations.is_phone\
+ (self.text[position_number.start():position_number.end()]):
+ if self.preserve_phone_country_code is False:
+ self.filtered_text = \
+ self.filtered_text[:position_number.start()] + "[PHONE]" +\
+ self.filtered_text[position_number.end():]
+
+ else:
+ compile_ = re.compile(regex_phonenumber, re.VERBOSE)
+ country_code = compile_.search(self.text).groups()
+ size_of_code = len(country_code[0])
+ if size_of_code > 1:
+ self.filtered_text = \
+ self.filtered_text[:position_number.start() + \
+ size_of_code] + " [FILTERED]" + \
+ self.filtered_text[position_number.end():]
+ else:
+ self.filtered_text = \
+ self.filtered_text[:position_number.start()] +\
+ "[PHONE]" + self.filtered_text[position_number.end():]
+
+ return self.filtered_text

Пламен обнови решението на 23.04.2014 13:23 (преди над 10 години)

import re
def check(regex, text):
if re.search(regex, text, re.VERBOSE):
return True
else:
return False
class Validations(object):
def is_email(text):
regex_email = r'''^(?:[\w][\+\-\_\.\w]*)@
(?:[\w][a-zA-Z\-]{0,61}[^W-]\.)+
(?:(?:[a-zA-Z]{2}\.)(?:[a-zA-Z]{2}$)|(?:[a-zA-Z]{2,3}$))'''
return check(regex_email, text)
def is_hostname(text):
- regex_host = r'''^(?:[\w][0-9a-zA-Z\-]{0,62}?\.)+
+ regex_host = r'''^(?:[\w][0-9a-zA-Z\-]{0,62}?(?<!\-)\.)+
(?:(?:[a-zA-Z]{2}$)|(?:[a-zA-Z]{2}\.[a-zA-Z]{2}$)|
(?:[a-zA-Z]{3}$)|([a-zA-Z]{3}\.[a-zA-Z]{2,3}$))'''
return check(regex_host, text)
def is_ip_address(text):
regex_ip = r'''^((([01]?[0-9]?[0-9]|[2]?[05]?[05])(\.)){3}
([01]?[0-9]?[0-9]|[2]?[05]?[05]))$'''
return check(regex_ip, text)
def is_number(text):
regex = r'^[-]?((?:[1-9][0-9]*\.?[0-9]+)$|(?:[0-9](?:$|\.[0-9]+)))'
return check(regex, text)
def is_integer(text):
regex_integer = r'^(?:[-]?[1-9][0-9]*)$|^(?:[-]?[0])$'
return check(regex_integer, text)
def is_date(text):
regex_date = r'''^(?:[0-9]{4}-(?:[0][1-9]|[1][0-2])-(?:[0][1-9]|
[1-2][0-9]|[3][01]))$'''
return check(regex_date, text)
def is_time(text):
regex_time = r'''^(?:([01][0-9]|[2][0-3]):([0-4][0-9]|[5][0-9]):
([0-4][0-9]|[5][0-9]))$'''
return check(regex_time, text)
def is_datetime(text):
date_time = re.split(r'[T| ]', text)
return Validations.is_date(date_time[0]) and \
Validations.is_time(date_time[1])
def is_phone(text):
regex_number = r'''^((?:(?:[0](?!0))|(?:(?:[0]{2}|\+)[1-9][0-9]{0,2})))
(?:[\ \-\(\)]{0,2}[0-9]){6,11}$'''
return check(regex_number, text)
class PrivacyFilter():
def __init__(self, text):
self.text = text
self.filtered_text = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.preserve_email_username = False
def filter_email(self, start, end):
all_regex = r'([^ ]+)@([^ ]+)'
self.filtered_text = self.text[:start]+"[EMAIL]"+self.text[end:]
def filter_username(self):
print("in")
left_regex = r'([^ ]+)@'
left_part = re.search(left_regex, self.text)
size_of_left_part = left_part.end() - left_part.start()-1
if size_of_left_part > 6:
result = self.text[:left_part.start()+3] + '[FILTERED]' + \
self.text[left_part.end()-1:]
else:
result = self.text[:left_part.start()] + '[FILTERED]' + \
self.text[left_part.end()-1:]
self.filtered_text = result
def filter_hostname(self):
left_regex = r'([^ ]+)@'
left_part = re.search(left_regex, self.text)
result = self.text[:left_part.start()] + '[FILTERED]' + \
self.text[left_part.end()-1:]
self.filtered_text = result
def filtered(self):
regex_mail = r'([^ ]+)@([^ ]+)'
position = re.search(regex_mail, self.text)
if position and Validations.is_email\
(self.text[position.start():position.end()]):
if self.preserve_email_username is False and \
self.preserve_email_hostname is False:
self.filter_email(position.start(), position.end())
elif self.preserve_email_username:
self.filter_username()
else:
self.filter_hostname()
regex_phonenumber = r'''((?:(?:[0](?!0))|(?:(?:[0]{2}|\+)
[1-9][0-9]{0,2})))(?:[\ \-\(\)]{0,2}[0-9]){6,11}'''
position_number = re.search(regex_phonenumber, self.text, re.VERBOSE)
if position_number:
if Validations.is_phone\
(self.text[position_number.start():position_number.end()]):
if self.preserve_phone_country_code is False:
self.filtered_text = \
self.filtered_text[:position_number.start()] + "[PHONE]" +\
self.filtered_text[position_number.end():]
else:
compile_ = re.compile(regex_phonenumber, re.VERBOSE)
country_code = compile_.search(self.text).groups()
size_of_code = len(country_code[0])
if size_of_code > 1:
self.filtered_text = \
self.filtered_text[:position_number.start() + \
size_of_code] + " [FILTERED]" + \
self.filtered_text[position_number.end():]
else:
self.filtered_text = \
self.filtered_text[:position_number.start()] +\
"[PHONE]" + self.filtered_text[position_number.end():]
return self.filtered_text