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

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

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

Резултати

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

Код

import re
USERNAME = r'([a-zA-Z\d][+a-zA-Z0-9-_\.]{,200})'
TLD = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
DOMAINNAME = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
EMAIL = USERNAME + '@(' + DOMAINNAME + r'\.)+' + TLD
HOSTNAME = '(' + DOMAINNAME + r'\.)+' + TLD
BYTES = r'(\d(?!\d)|[1-9]\d(?!\d)|1\d{2}|2([0-4]\d|5[0-5]))'
IP = BYTES + r'\.' + BYTES + r'\.' + BYTES + r'\.' + BYTES
DOUBLE = r'-?(0|([1-9]\d*))(\.\d+)?'
INTEGER = r'-?(0|([1-9]\d*))'
DATE = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1\d|3[01])'
TIME = r'([01]\d|2[0-3]):[0-5][0-9]:[0-5][0-9]'
DATETIME = DATE + r'[ T]' + TIME
PHONE = r'(0(?!0)|((00|\+)[1-9]\d{0,2}))([ )\(\-]{,2}\d){6,11}'
INTERNATIONAL_NUMBER = r'((00|\+)[1-9]\d{0,2})([ )\(\-]{,2}\d){6,11}'
def matcher(regex, string):
matches = re.match(regex + '$', string)
return matches is not None
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):
filtered_lbl = '[FILTERED]'
email_lbl = '[EMAIL]'
phone_lbl = '[PHONE]'
username_email = USERNAME + r'(?=@(' + DOMAINNAME + r'\.)' + TLD + ')'
result = self.text
if self.partially_preserve_email_username:
def f(match):
if(len(match.group())) < 6:
return filtered_lbl
return match.group()[:3] + filtered_lbl
result = re.sub(username_email, f, result)
elif self.preserve_email_hostname:
result = re.sub(username_email, filtered_lbl, result)
else:
result = re.sub(r'\b' + EMAIL + r'\b', email_lbl, result)
if self.preserve_phone_country_code:
result = re.sub(INTERNATIONAL_NUMBER,
lambda match: match.group(1) + ' ' + filtered_lbl,
result)
result = re.sub(PHONE, phone_lbl, result)
return result
class Validations:
@classmethod
def is_email(cls, value):
return matcher(EMAIL, value)
@classmethod
def is_phone(cls, value):
return matcher(PHONE, value)
@classmethod
def is_hostname(cls, value):
return matcher(HOSTNAME, value)
@classmethod
def is_ip_address(cls, value):
return matcher(IP, value)
@classmethod
def is_number(cls, value):
return matcher(DOUBLE, value)
@classmethod
def is_integer(cls, value):
return matcher(INTEGER, value)
@classmethod
def is_date(cls, value):
return matcher(DATE, value)
@classmethod
def is_time(cls, value):
return matcher(TIME, value)
@classmethod
def is_datetime(cls, value):
return matcher(DATETIME, value)

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

...FF...........F.....FF..FF...........
======================================================================
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-c0aqqu/test.py", line 50, in test_does_not_filter_invalid_emails
    self.assertEqual(text, self.filter_email_usernames(text))
AssertionError: 'Contact me here: _invalid@email.com' != 'Contact me here: _[FILTERED]@email.com'
- Contact me here: _invalid@email.com
?                   ^^^^^^^
+ Contact me here: _[FILTERED]@email.com
?                   ^^^^^^^^^^


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


======================================================================
FAIL: test_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-c0aqqu/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-c0aqqu/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-c0aqqu/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-c0aqqu/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-c0aqqu/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

----------------------------------------------------------------------
Ran 39 tests in 0.046s

FAILED (failures=7)

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

Мария обнови решението на 19.04.2014 17:46 (преди около 10 години)

+import re
+
+
+def matcher(regex, string):
+ matches = re.match(regex, string)
+ if matches is None:
+ return False
+ return True
+
+
+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):
+ filtered_lbl = '[FILTERED]'
+ email_lbl = '[EMAIL]'
+ phone_lbl = '[PHONE]'
+ username = r'([a-zA-Z\d][+a-zA-Z0-9-_\.]{,200})'
+ tld = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
+ domainname = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
+ email = username + '@(' + domainname + r'\.)+' + tld
+ username_email = username + \
+ r'(?=@(' + domainname + r'\.)' + tld + r')'
+ phone = r'(0(?!0)|((00|\+)[1-9]\d{0,2}))([ )\(\-]{,2}\d){6,11}'
+
+ result = self.text
+
+ if self.partially_preserve_email_username:
+ def f(match):
+ if(len(match.group())) < 6:
+ return filtered_lbl
+ return match.group()[:3] + filtered_lbl
+ result = re.sub(username_email, f, result)
+ elif self.preserve_email_hostname:
+ result = re.sub(username_email, filtered_lbl, result)
+ else:
+ result = re.sub(email, email_lbl, result)
+ if self.preserve_phone_country_code:
+ result = re.sub(phone,
+ lambda match: match.group(2) + ' ' + filtered_lbl,
+ result)
+ result = re.sub(phone, phone_lbl, result)
+ return result
+
+
+class Validations:
+
+ @classmethod
+ def is_email(cls, value):
+ username = r'([a-zA-Z\d][+a-zA-Z0-9-_\.]{,200})'
+ tld = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
+ domainname = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
+ email = username + '@(' + domainname + r'\.)+' + tld + '$'
+ return matcher(email, value)
+
+ @classmethod
+ def is_phone(cls, value):
+ phone = r'(0(?!0)|((00|\+)[1-9]\d{0,2}))([ )\(\-]{,2}\d){6,11}$'
+ return matcher(phone, value)
+
+ @classmethod
+ def is_hostname(cls, value):
+ tld = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
+ domainname = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
+ hostname = '(' + domainname + r'\.)+' + tld + '$'
+ return matcher(hostname, value)
+
+ @classmethod
+ def is_ip_address(cls, value):
+ bytes = r'(\d(?!\d)|[1-9]\d(?!\d)|1\d{2}|2([0-4]\d|5[0-5]))'
+ ip = bytes + r'\.' + bytes + r'\.' + bytes + r'\.' + bytes + '$'
+ return matcher(ip, value)
+
+ @classmethod
+ def is_number(cls, value):
+ double = r'-?(0|([1-9]\d*))(\.\d+)?$'
+ return matcher(double, value)
+
+ @classmethod
+ def is_integer(cls, value):
+ integer = r'-?(0|([1-9]\d*))$'
+ return matcher(integer, value)
+
+ @classmethod
+ def is_date(cls, value):
+ date = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1\d|3[01])$'
+ return matcher(date, value)
+
+ @classmethod
+ def is_time(cls, value):
+ time = r'([01]\d|2[0-3]):[0-5][0-9]:[0-5][0-9]$'
+ return matcher(time, value)
+
+ @classmethod
+ def is_datetime(cls, value):
+ date = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1\d|3[01])'
+ time = r'([01]\d|2[0-3]):[0-5][0-9]:[0-5][0-9]'
+ datetime = date + r'[ T]' + time + r'$'
+ return matcher(datetime, value)

Мария обнови решението на 23.04.2014 12:49 (преди около 10 години)

import re
+USERNAME = r'([a-zA-Z\d][+a-zA-Z0-9-_\.]{,200})'
+TLD = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
+DOMAINNAME = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
+EMAIL = USERNAME + '@(' + DOMAINNAME + r'\.)+' + TLD
+HOSTNAME = '(' + DOMAINNAME + r'\.)+' + TLD
+BYTES = r'(\d(?!\d)|[1-9]\d(?!\d)|1\d{2}|2([0-4]\d|5[0-5]))'
+IP = BYTES + r'\.' + BYTES + r'\.' + BYTES + r'\.' + BYTES
+DOUBLE = r'-?(0|([1-9]\d*))(\.\d+)?'
+INTEGER = r'-?(0|([1-9]\d*))'
+DATE = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1\d|3[01])'
+TIME = r'([01]\d|2[0-3]):[0-5][0-9]:[0-5][0-9]'
+DATETIME = DATE + r'[ T]' + TIME
+PHONE = r'(0(?!0)|((00|\+)[1-9]\d{0,2}))([ )\(\-]{,2}\d){6,11}'
+INTERNATIONAL_NUMBER = r'((00|\+)[1-9]\d{0,2})([ )\(\-]{,2}\d){6,11}'
+
+
def matcher(regex, string):
- matches = re.match(regex, string)
- if matches is None:
- return False
- return True
+ matches = re.match(regex + '$', string)
+ return matches is not None
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):
filtered_lbl = '[FILTERED]'
email_lbl = '[EMAIL]'
phone_lbl = '[PHONE]'
- username = r'([a-zA-Z\d][+a-zA-Z0-9-_\.]{,200})'
- tld = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
- domainname = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
- email = username + '@(' + domainname + r'\.)+' + tld
- username_email = username + \
- r'(?=@(' + domainname + r'\.)' + tld + r')'
- phone = r'(0(?!0)|((00|\+)[1-9]\d{0,2}))([ )\(\-]{,2}\d){6,11}'
-
+ username_email = USERNAME + r'(?=@(' + DOMAINNAME + r'\.)' + TLD + ')'
result = self.text
if self.partially_preserve_email_username:
def f(match):
if(len(match.group())) < 6:
return filtered_lbl
return match.group()[:3] + filtered_lbl
result = re.sub(username_email, f, result)
elif self.preserve_email_hostname:
result = re.sub(username_email, filtered_lbl, result)
else:
- result = re.sub(email, email_lbl, result)
+ result = re.sub(r'\b' + EMAIL + r'\b', email_lbl, result)
if self.preserve_phone_country_code:
- result = re.sub(phone,
- lambda match: match.group(2) + ' ' + filtered_lbl,
+ result = re.sub(INTERNATIONAL_NUMBER,
+ lambda match: match.group(1) + ' ' + filtered_lbl,
result)
- result = re.sub(phone, phone_lbl, result)
+ result = re.sub(PHONE, phone_lbl, result)
return result
class Validations:
@classmethod
def is_email(cls, value):
- username = r'([a-zA-Z\d][+a-zA-Z0-9-_\.]{,200})'
- tld = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
- domainname = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
- email = username + '@(' + domainname + r'\.)+' + tld + '$'
- return matcher(email, value)
+ return matcher(EMAIL, value)
@classmethod
def is_phone(cls, value):
- phone = r'(0(?!0)|((00|\+)[1-9]\d{0,2}))([ )\(\-]{,2}\d){6,11}$'
- return matcher(phone, value)
+ return matcher(PHONE, value)
@classmethod
def is_hostname(cls, value):
- tld = r'[a-zA-Z]{2,3}(.\[a-zA-Z]{2,3})?'
- domainname = r'[a-zA-Z\d]([a-zA-Z\d-]{,61}[a-zA-Z\d])?'
- hostname = '(' + domainname + r'\.)+' + tld + '$'
- return matcher(hostname, value)
+ return matcher(HOSTNAME, value)
@classmethod
def is_ip_address(cls, value):
- bytes = r'(\d(?!\d)|[1-9]\d(?!\d)|1\d{2}|2([0-4]\d|5[0-5]))'
- ip = bytes + r'\.' + bytes + r'\.' + bytes + r'\.' + bytes + '$'
- return matcher(ip, value)
+ return matcher(IP, value)
@classmethod
def is_number(cls, value):
- double = r'-?(0|([1-9]\d*))(\.\d+)?$'
- return matcher(double, value)
+ return matcher(DOUBLE, value)
@classmethod
def is_integer(cls, value):
- integer = r'-?(0|([1-9]\d*))$'
- return matcher(integer, value)
+ return matcher(INTEGER, value)
@classmethod
def is_date(cls, value):
- date = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1\d|3[01])$'
- return matcher(date, value)
+ return matcher(DATE, value)
@classmethod
def is_time(cls, value):
- time = r'([01]\d|2[0-3]):[0-5][0-9]:[0-5][0-9]$'
- return matcher(time, value)
+ return matcher(TIME, value)
@classmethod
def is_datetime(cls, value):
- date = r'\d{4}-(0[1-9]|1[0-2])-(0[1-9]|1\d|3[01])'
- time = r'([01]\d|2[0-3]):[0-5][0-9]:[0-5][0-9]'
+ return matcher(DATETIME, value)
- datetime = date + r'[ T]' + time + r'$'
- return matcher(datetime, value)