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

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

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

Резултати

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

Код

import re
class Validations:
@classmethod
def is_email(cls, value):
mail = value.split('@')
res = re.match(r"[a-zA-Z0-9][a-zA-Z0-9_+.-]{,200}", mail[0])
valid_user = res.group() == mail[0]
valid_host = Validations.is_hostname(mail[1])
return valid_user and valid_host
@classmethod
def is_phone(cls, value):
result = re.match(
r"(0|((00|\+)(?!0)[0-9]{1,3}))" #prefix
r"[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<![\s\-\(\)])",
value)
if result:
if result.group() == value:
return True
return False
@classmethod
def is_hostname(cls, value):
result = re.match(
r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+" #(sub)domain
r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?", #TLD
value
)
if result:
return result.group() == value
else:
return False
@classmethod
def is_ip_address(cls, value):
result = re.match(
r"^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
r"(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$",
value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_number(cls, value):
result = re.match(r"-?(0|[1-9][0-9]*)(\.[0-9]+)?", value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_integer(cls, value):
result = re.match(r"-?(0|[1-9][0-9]*)", value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_date(cls, value):
expr = r"[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])"
result = re.match(expr, value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_time(cls, value):
expr = r"([01][0-9]|2[0-3]):([0-4][0-9]|5[0-9]):([0-4][0-9]|5[0-9])"
result = re.match(expr, value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_datetime(cls, value):
values = re.split(' |T',value)
if len(values) == 2:
valid_date = Validations.is_date(values[0])
valid_time = Validations.is_time(values[1])
return valid_date and valid_time
return False
class PrivacyFilter:
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
def filtered(self):
filtered_text = self.text
def mask_email(matchobj):
email = matchobj.group().split('@')
if self.partially_preserve_email_username:
if len(email[0]) < 6:
return "[FILTERED]@%s" %(email[1])
else:
return "%s[FILTERED]@%s" %(email[0][:3], email[1])
elif self.preserve_email_hostname:
return "[FILTERED]@%s" %(email[1])
else:
return "[EMAIL]"
filtered_text = re.sub(
r"([a-zA-Z0-9][a-zA-Z0-9_+.-]{,200})@"
r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
mask_email,
filtered_text
)
if self.preserve_phone_country_code:
filtered_text = re.sub(
r"((00|\+)(?!0)[0-9]{1,3})"
r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<![\s\-\(\)]))",
r"\1 [FILTERED]",
filtered_text
)
else:
filtered_text = re.sub(
r"((00|\+)(?!0)[0-9]{1,3})"
r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<![\s\-\(\)]))",
"[PHONE]",
filtered_text
)
filtered_text = re.sub(
r"0[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<![\s\-\(\)])",
'[PHONE]',
filtered_text
)
return filtered_text

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

...FF..........EF......................
======================================================================
ERROR: 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-1vgcjwm/test.py", line 124, in test_can_validate_more_complex_emails
    self.assertIs(solution.Validations.is_email(email), valid)
  File "/tmp/d20140513-11348-1vgcjwm/solution.py", line 10, in is_email
    valid_user = res.group() == mail[0]
AttributeError: 'NoneType' object has no attribute 'group'

======================================================================
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-1vgcjwm/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-1vgcjwm/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_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-1vgcjwm/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

----------------------------------------------------------------------
Ran 39 tests in 0.042s

FAILED (failures=3, errors=1)

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

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

+import re
+
+
+class Validations:
+
+ @classmethod
+ def is_email(cls, value):
+ mail = value.split('@')
+ res = re.match(r"[a-zA-Z0-9][a-zA-Z0-9_+.-]{,200}", mail[0])
+ valid_user = res.group() == mail[0]
+ valid_host = Validations.is_hostname(mail[1])
+ return valid_user and valid_host
+
+ @classmethod
+ def is_phone(cls, value):
+ try_local = re.match(
+ r"0[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())",
+ value
+ )
+ try_global = re.match(
+ r"(00|\+)((?!0)[0-9]{1,3})" #global prefix
+ r"[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())", #number
+ value)
+ if try_local:
+ if try_local.group() == value:
+ return True
+ if try_global:
+ if try_global.group() == value:
+ return True
+ return False
+
+ @classmethod
+ def is_hostname(cls, value):
+ result = re.match(
+ r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+" #(sub)domain
+ r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?", #TLD
+ value
+ )
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_ip_address(cls, value):
+ result = re.match(
+ r"^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
+ r"(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$",
+ value)
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_number(cls, value):
+ result = re.match(r"-?(0|[1-9][0-9]*)(\.[0-9]+)?", value)
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_integer(cls, value):
+ result = re.match(r"-?(0|[1-9])[0-9]*", value)
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_date(cls, value):
+ expr = r"[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])"
+ result = re.match(expr, value)
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_time(cls, value):
+ expr = r"([01][0-9]|2[0-3]):([0-4][0-9]|5[0-9]):([0-4][0-9]|5[0-9])"
+ result = re.match(expr, value)
+ if result:
+ return result.group() == value
+ else:
+ return False
+
+ @classmethod
+ def is_datetime(cls, value):
+ values = re.split(' |T',value)
+ if len(values) == 2:
+ valid_date = Validations.is_date(values[0])
+ valid_time = Validations.is_time(values[1])
+ return valid_date and valid_time
+ return False
+
+
+class PrivacyFilter:
+
+ preserve_phone_country_code = False
+ preserve_email_hostname = False
+ partially_preserve_email_username = False
+
+ def __init__(self, text):
+ self.text = text
+
+ def filtered(self):
+ filtered_text = self.text
+ def masK_username(matchobj):
+ email = matchobj.group().split('@')
+ if len(email[0]) < 6:
+ return "[FILTERED]"
+ else:
+ return email[0][:3] + "[FILTERED]@" + email[1]
+
+ if self.partially_preserve_email_username:
+ filtered_text = re.sub(
+ r"([a-zA-Z0-9][a-zA-Z0-9_+.-]{,200})@"
+ r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
+ r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
+ masK_username,
+ filtered_text
+ )
+ elif self.preserve_email_hostname:
+ filtered_text = re.sub(
+ r"([a-zA-Z0-9][a-zA-Z0-9_+.-]{,200})@"
+ r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
+ r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
+ r"[FILTERED]@\2\3",
+ filtered_text
+ )
+ else:
+ filtered_text = re.sub(
+ r"[a-zA-Z0-9][a-zA-Z0-9_+.-]{,200}@"
+ r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
+ r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
+ "[EMAIL]",
+ filtered_text
+ )
+ if self.preserve_phone_country_code:
+ filtered_text = re.sub(
+ r"(00|\+)((?!0)[0-9]{1,3})"
+ r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-()))",
+ r"\1\2 [FILTERED]",
+ filtered_text
+ )
+ else:
+ filtered_text = re.sub(
+ r"(00|\+)((?!0)[0-9]{1,3})"
+ r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-()))",
+ "[PHONE]",
+ filtered_text
+ )
+ filtered_text = re.sub(
+ r"0[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())",
+ '[PHONE]',
+ filtered_text
+ )
+ return filtered_text
+

Георги обнови решението на 23.04.2014 02:03 (преди над 10 години)

import re
class Validations:
@classmethod
def is_email(cls, value):
mail = value.split('@')
res = re.match(r"[a-zA-Z0-9][a-zA-Z0-9_+.-]{,200}", mail[0])
valid_user = res.group() == mail[0]
valid_host = Validations.is_hostname(mail[1])
return valid_user and valid_host
@classmethod
def is_phone(cls, value):
try_local = re.match(
r"0[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())",
value
)
try_global = re.match(
r"(00|\+)((?!0)[0-9]{1,3})" #global prefix
r"[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())", #number
value)
if try_local:
if try_local.group() == value:
return True
if try_global:
if try_global.group() == value:
return True
return False
@classmethod
def is_hostname(cls, value):
result = re.match(
r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+" #(sub)domain
r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?", #TLD
value
)
if result:
return result.group() == value
else:
return False
@classmethod
def is_ip_address(cls, value):
result = re.match(
r"^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
r"(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$",
value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_number(cls, value):
result = re.match(r"-?(0|[1-9][0-9]*)(\.[0-9]+)?", value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_integer(cls, value):
- result = re.match(r"-?(0|[1-9])[0-9]*", value)
+ result = re.match(r"-?(0|[1-9][0-9]*)", value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_date(cls, value):
expr = r"[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])"
result = re.match(expr, value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_time(cls, value):
expr = r"([01][0-9]|2[0-3]):([0-4][0-9]|5[0-9]):([0-4][0-9]|5[0-9])"
result = re.match(expr, value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_datetime(cls, value):
values = re.split(' |T',value)
if len(values) == 2:
valid_date = Validations.is_date(values[0])
valid_time = Validations.is_time(values[1])
return valid_date and valid_time
return False
class PrivacyFilter:
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
def filtered(self):
filtered_text = self.text
def masK_username(matchobj):
email = matchobj.group().split('@')
if len(email[0]) < 6:
return "[FILTERED]"
else:
return email[0][:3] + "[FILTERED]@" + email[1]
if self.partially_preserve_email_username:
filtered_text = re.sub(
r"([a-zA-Z0-9][a-zA-Z0-9_+.-]{,200})@"
r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
masK_username,
filtered_text
)
elif self.preserve_email_hostname:
filtered_text = re.sub(
r"([a-zA-Z0-9][a-zA-Z0-9_+.-]{,200})@"
r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
r"[FILTERED]@\2\3",
filtered_text
)
else:
filtered_text = re.sub(
r"[a-zA-Z0-9][a-zA-Z0-9_+.-]{,200}@"
r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
"[EMAIL]",
filtered_text
)
if self.preserve_phone_country_code:
filtered_text = re.sub(
r"(00|\+)((?!0)[0-9]{1,3})"
r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-()))",
r"\1\2 [FILTERED]",
filtered_text
)
else:
filtered_text = re.sub(
r"(00|\+)((?!0)[0-9]{1,3})"
r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-()))",
"[PHONE]",
filtered_text
)
filtered_text = re.sub(
r"0[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())",
'[PHONE]',
filtered_text
)
return filtered_text

Георги обнови решението на 23.04.2014 08:21 (преди над 10 години)

import re
class Validations:
@classmethod
def is_email(cls, value):
mail = value.split('@')
res = re.match(r"[a-zA-Z0-9][a-zA-Z0-9_+.-]{,200}", mail[0])
valid_user = res.group() == mail[0]
valid_host = Validations.is_hostname(mail[1])
return valid_user and valid_host
@classmethod
def is_phone(cls, value):
- try_local = re.match(
- r"0[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())",
- value
- )
- try_global = re.match(
- r"(00|\+)((?!0)[0-9]{1,3})" #global prefix
- r"[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())", #number
+ result = re.match(
+ r"(0|((00|\+)(?!0)[0-9]{1,3}))" #prefix
+ r"[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<![\s\-\(\)])",
value)
- if try_local:
- if try_local.group() == value:
+ if result:
+ if result.group() == value:
return True
- if try_global:
- if try_global.group() == value:
- return True
return False
@classmethod
def is_hostname(cls, value):
result = re.match(
r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+" #(sub)domain
r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?", #TLD
value
)
if result:
return result.group() == value
else:
return False
@classmethod
def is_ip_address(cls, value):
result = re.match(
r"^([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])"
r"(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$",
value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_number(cls, value):
result = re.match(r"-?(0|[1-9][0-9]*)(\.[0-9]+)?", value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_integer(cls, value):
result = re.match(r"-?(0|[1-9][0-9]*)", value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_date(cls, value):
expr = r"[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[0-1])"
result = re.match(expr, value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_time(cls, value):
expr = r"([01][0-9]|2[0-3]):([0-4][0-9]|5[0-9]):([0-4][0-9]|5[0-9])"
result = re.match(expr, value)
if result:
return result.group() == value
else:
return False
@classmethod
def is_datetime(cls, value):
values = re.split(' |T',value)
if len(values) == 2:
valid_date = Validations.is_date(values[0])
valid_time = Validations.is_time(values[1])
return valid_date and valid_time
return False
class PrivacyFilter:
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
def __init__(self, text):
self.text = text
def filtered(self):
filtered_text = self.text
- def masK_username(matchobj):
+
+ def mask_email(matchobj):
email = matchobj.group().split('@')
- if len(email[0]) < 6:
- return "[FILTERED]"
+ if self.partially_preserve_email_username:
+ if len(email[0]) < 6:
+ return "[FILTERED]@%s" %(email[1])
+ else:
+ return "%s[FILTERED]@%s" %(email[0][:3], email[1])
+ elif self.preserve_email_hostname:
+ return "[FILTERED]@%s" %(email[1])
else:
- return email[0][:3] + "[FILTERED]@" + email[1]
+ return "[EMAIL]"
- if self.partially_preserve_email_username:
- filtered_text = re.sub(
- r"([a-zA-Z0-9][a-zA-Z0-9_+.-]{,200})@"
- r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
- r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
- masK_username,
- filtered_text
- )
- elif self.preserve_email_hostname:
- filtered_text = re.sub(
- r"([a-zA-Z0-9][a-zA-Z0-9_+.-]{,200})@"
- r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
- r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
- r"[FILTERED]@\2\3",
- filtered_text
- )
- else:
- filtered_text = re.sub(
- r"[a-zA-Z0-9][a-zA-Z0-9_+.-]{,200}@"
- r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
- r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
- "[EMAIL]",
- filtered_text
- )
+ filtered_text = re.sub(
+ r"([a-zA-Z0-9][a-zA-Z0-9_+.-]{,200})@"
+ r"((?!-)[a-zA-Z0-9-]{1,63}(?<!-)\.)+"
+ r"([a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)",
+ mask_email,
+ filtered_text
+ )
if self.preserve_phone_country_code:
filtered_text = re.sub(
- r"(00|\+)((?!0)[0-9]{1,3})"
- r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-()))",
- r"\1\2 [FILTERED]",
+ r"((00|\+)(?!0)[0-9]{1,3})"
+ r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<![\s\-\(\)]))",
+ r"\1 [FILTERED]",
filtered_text
)
else:
filtered_text = re.sub(
- r"(00|\+)((?!0)[0-9]{1,3})"
- r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-()))",
+ r"((00|\+)(?!0)[0-9]{1,3})"
+ r"([\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<![\s\-\(\)]))",
"[PHONE]",
filtered_text
)
filtered_text = re.sub(
- r"0[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<!\s-())",
+ r"0[\s\-\(\)]{,2}([0-9][\s\-\(\)]{,2}){6,11}(?<![\s\-\(\)])",
'[PHONE]',
filtered_text
)
return filtered_text