Решение на Регулярни изрази от Владимир Начев

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

Към профила на Владимир Начев

Резултати

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

Код

import re
HOST_NAME = r"(\w(\w|-){,61}\w\.)+"
HOST_TLD = r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,2})?"
HOST = HOST_NAME + HOST_TLD
EMAIL_ID = r"\w(\w|_|\+|\.|-){,200}"
EMAIL = EMAIL_ID + "@" + HOST
PHONE = r"(\(|\))?\d([ \-\(\)]{,2}\d){4,9}\d"
CODE = r"(\b00|\+)[1-9]\d{,2}"
INTERNATIONAL = CODE + PHONE
LOCAL = r"\b0(?=[^0])" + PHONE
BYTE_NUMBER = \
r"\b((0)|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([1-9]))\b"
IP_ADDRESS = r"^" + BYTE_NUMBER + r'\.' + BYTE_NUMBER + r'\.' +\
BYTE_NUMBER + r'\.' + BYTE_NUMBER + r"$"
INT_NUMBER = r"^-?(0|[1-9][0-9]*)$"
NUMBER = r"^-?(0|[1-9][0-9]*)(\.[0-9]+)?$"
DATE = r"\d\d\d\d-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\d)|(30)|(31))"
HOUR = r"(([0-1]\d)|(2[0-3]))(:[0-5]\d)(:[0-5]\d)"
DATE_AND_HOUR = r"^" + DATE + r" |T" + HOUR + r"$"
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):
if not self.preserve_email_hostname and \
not self.preserve_phone_country_code and \
not self.partially_preserve_email_username:
result = re.sub(EMAIL, "[EMAIL]", self.text)
result = re.sub(INTERNATIONAL, "[PHONE]", result)
result = re.sub(LOCAL, "[PHONE]", result)
return result
result = self.text
if self.partially_preserve_email_username:
self.preserve_email_hostname = True
while re.search(EMAIL, result):
start, end = re.search(EMAIL, result).span()
end1 = re.search(EMAIL_ID, result[start:end]).end()
if end1 - start >= 6:
result = result[:start+3] +\
"[FILTERED]" +\
result[end1:]
if self.preserve_email_hostname:
while re.search(EMAIL, result):
start, end = re.search(EMAIL, result).span()
result = result[:start] +\
re.sub(EMAIL_ID + r"@", "[FILTERED]@", result[start:end]) +\
result[end:]
if self.preserve_phone_country_code:
while re.search(INTERNATIONAL, result):
start, end = re.search(INTERNATIONAL, result).span()
start1, end1 = re.search(CODE, result[start:end]).span()
result = result[:end1+start] +\
" [FILTERED]" + result[end:]
return result
class Validations:
@classmethod
def is_email(cls, value):
return bool(re.search(r"^" + EMAIL + r"$", value))
@classmethod
def is_phone(cls, value):
return re.search(r"^" + LOCAL + r"$", value) or\
re.search(r"^" + INTERNATIONAL + r"$", value)
@classmethod
def is_hostname(cls, value):
return bool(re.search(r"^" + HOST + r"$", value))
@classmethod
def is_ip_address(cls, value):
return bool(re.search(IP_ADDRESS, value))
@classmethod
def is_number(cls, value):
return bool(re.search(NUMBER, value))
@classmethod
def is_integer(cls, value):
return bool(re.search(INT_NUMBER, value))
@classmethod
def is_date(cls, value):
return bool(re.search(r"^" + DATE + r"$", value))
@classmethod
def is_time(cls, value):
return bool(re.search(r"^" + HOUR + r"$", value))
@classmethod
def is_datetime(cls, value):
return bool(re.search(DATE_AND_HOUR, value))

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

..EFFFEF.F.....FF.....FF..FF...FF......
======================================================================
ERROR: test_does_not_brake_with_unicode (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 57, in thread
    raise TimeoutError
TimeoutError

======================================================================
ERROR: test_filters_whole_email_usernames_if_too_short (test.PrivacyFilterTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "lib/language/python/runner.py", line 57, in thread
    raise TimeoutError
TimeoutError

======================================================================
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-58fj9k/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-58fj9k/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_more_complex_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-58fj9k/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[PHONE]' != '+1 555 123-456'
- [PHONE]
+ +1 555 123-456


======================================================================
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-58fj9k/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_preserves_whitespace_around_phones (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-58fj9k/test.py", line 89, in test_preserves_whitespace_around_phones
    self.assertEqual(' [PHONE] or...', solution.PrivacyFilter(' +359881212-12-1 2 or...').filtered())
AssertionError: ' [PHONE] or...' != ' [PHONE]-1 2 or...'
-  [PHONE] or...
+  [PHONE]-1 2 or...
?         ++++


======================================================================
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-58fj9k/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-58fj9k/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-58fj9k/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-58fj9k/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-58fj9k/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-58fj9k/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_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-58fj9k/test.py", line 281, in test_validates_datetime_values
    self.assertFalse(solution.Validations.is_datetime('2012-00-19T23:59:00'))
AssertionError: True is not false

======================================================================
FAIL: test_validates_hostnames (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-58fj9k/test.py", line 169, in test_validates_hostnames
    self.assertTrue(solution.Validations.is_hostname('1.2.3.4.xip.io'))
AssertionError: False is not true

----------------------------------------------------------------------
Ran 39 tests in 5.849s

FAILED (failures=13, errors=2)

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

Владимир обнови решението на 23.04.2014 15:57 (преди над 10 години)

+import re
+
+HOST_NAME = r"(\w(\w|-){,61}\w\.)+"
+HOST_TLD = r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,2})?"
+HOST = HOST_NAME + HOST_TLD
+
+EMAIL = r"^\w(\w|_|\+|\.|-){,200}@" + HOST + r"$"
+
+PHONE = r"(\(|\))?\d([ \-\(\)]{,2}\d){4,9}\d"
+CODE = r"(\b00|\+)[1-9]\d{,2}"
+INTERNATIONAL = CODE + PHONE
+LOCAL = r"\b0(?=[^0])" + PHONE
+
+BYTE_NUMBER = \
+ r"\b((0)|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([1-9]))\b"
+IP_ADDRESS = r"^" + BYTE_NUMBER + r'\.' + BYTE_NUMBER + r'\.' +\
+ BYTE_NUMBER + r'\.' + BYTE_NUMBER + r"$"
+
+INT_NUMBER = r"^-?(0|[1-9][0-9]*)$"
+
+NUMBER = r"^-?(0|[1-9][0-9]*)(\.[0-9]+)?$"
+
+DATE = r"\d\d\d\d-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\d)|(30)|(31))"
+
+HOUR = r"(([0-1]\d)|(2[0-3]))(:[0-5]\d)(:[0-5]\d)"
+
+DATE_AND_HOUR = r"^" + DATE + r" |T" + HOUR + r"$"
+
+class PrivacyFilter:
+ def __init__(self, text):
+ self.text = text.copy()
+ self.preserve_phone_country_code = False
+ self.preserve_email_hostname = False
+ self.partially_preserve_email_username = False
+
+ def filtered(self):
+ if not self.preserve_email_hostname and \
+ not self.preserve_phone_country_code and \
+ not self.partially_preserve_email_username:
+ result = re.sub(EMAIL,"[EMAIL]", self.text)
+ result = re.sub(INTERNATIONAL, "[PHONE]",result)
+ result = re.sub(LOCAL, "[PHONE]", result)
+ return result
+ result = ""
+ if self.partially_preserve_email_username:
+ pass
+
+ if self.preserve_email_hostname:
+ pass
+
+ if self.preserve_phone_country_code:
+ pass
+
+
+
+class Validations:
+ @classmethod
+ def is_email(cls,value):
+ return bool(re.search(r"^" + EMAIL + r"$",value))
+
+ @classmethod
+ def is_phone(cls,value):
+ return re.search(r"^" + LOCAL + r"$", value) or\
+ re.search(r"^" + INTERNATIONAL + r"$",value)
+
+ @classmethod
+ def is_hostname(cls,value):
+ return bool(re.search(r"^" + HOST + r"$", value))
+
+ @classmethod
+ def is_ip_address(cls,value):
+ return bool(re.search(IP_ADDRESS,value))
+
+ @classmethod
+ def is_number(cls,value):
+ return bool(re.search(NUMBER,value))
+
+ @classmethod
+ def is_integer(cls,value):
+ return bool(re.search(INT_NUMBER, value))
+
+ @classmethod
+ def is_date(cls,value):
+ return bool(re.search(r"^" + DATE + r"$",value))
+
+ @classmethod
+ def is_time(cls,value):
+ return bool(re.search(r"^" + HOUR + r"$",value))
+
+ @classmethod
+ def is_datetime(cls,value):
+ return bool(re.search(DATE_AND_HOUR,value))

Владимир обнови решението на 23.04.2014 16:44 (преди над 10 години)

import re
HOST_NAME = r"(\w(\w|-){,61}\w\.)+"
HOST_TLD = r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,2})?"
HOST = HOST_NAME + HOST_TLD
-EMAIL = r"^\w(\w|_|\+|\.|-){,200}@" + HOST + r"$"
+EMAIL_ID = r"\w(\w|_|\+|\.|-){,200}"
+EMAIL = EMAIL_ID + "@" + HOST
PHONE = r"(\(|\))?\d([ \-\(\)]{,2}\d){4,9}\d"
-CODE = r"(\b00|\+)[1-9]\d{,2}"
+CODE = r"(\b00|\+)[1-9]\d{,2}"
INTERNATIONAL = CODE + PHONE
LOCAL = r"\b0(?=[^0])" + PHONE
BYTE_NUMBER = \
r"\b((0)|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([1-9]))\b"
IP_ADDRESS = r"^" + BYTE_NUMBER + r'\.' + BYTE_NUMBER + r'\.' +\
BYTE_NUMBER + r'\.' + BYTE_NUMBER + r"$"
INT_NUMBER = r"^-?(0|[1-9][0-9]*)$"
NUMBER = r"^-?(0|[1-9][0-9]*)(\.[0-9]+)?$"
DATE = r"\d\d\d\d-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\d)|(30)|(31))"
HOUR = r"(([0-1]\d)|(2[0-3]))(:[0-5]\d)(:[0-5]\d)"
DATE_AND_HOUR = r"^" + DATE + r" |T" + HOUR + r"$"
+
class PrivacyFilter:
def __init__(self, text):
- self.text = text.copy()
+ self.text = text
self.preserve_phone_country_code = False
self.preserve_email_hostname = False
self.partially_preserve_email_username = False
def filtered(self):
if not self.preserve_email_hostname and \
- not self.preserve_phone_country_code and \
- not self.partially_preserve_email_username:
- result = re.sub(EMAIL,"[EMAIL]", self.text)
- result = re.sub(INTERNATIONAL, "[PHONE]",result)
+ not self.preserve_phone_country_code and \
+ not self.partially_preserve_email_username:
+ result = re.sub(EMAIL, "[EMAIL]", self.text)
+ result = re.sub(INTERNATIONAL, "[PHONE]", result)
result = re.sub(LOCAL, "[PHONE]", result)
return result
- result = ""
+
+ result = self.text
+
if self.partially_preserve_email_username:
- pass
+ self.preserve_email_hostname = True
+ while re.search(EMAIL, result):
+ start, end = re.search(EMAIL, result).span()
+ end1 = re.search(EMAIL_ID, result[start:end]).end()
+ if end1 - start >= 6:
+ result = result[:start+3] +\
+ "[FILTERED]" +\
+ result[end1:]
if self.preserve_email_hostname:
- pass
+ while re.search(EMAIL, result):
+ start, end = re.search(EMAIL, result).span()
+ result = result[:start] +\
+ re.sub(EMAIL_ID, " [FILTERED]", result[start:end]) +\
+ result[end:]
if self.preserve_phone_country_code:
- pass
+ while re.search(INTERNATIONAL, result):
+ start, end = re.search(INTERNATIONAL, result).span()
+ result = result[:start] + \
+ re.sub(PHONE, " [FILTERED]", result[start:end]) + result[end:]
+ return result
-
class Validations:
@classmethod
- def is_email(cls,value):
- return bool(re.search(r"^" + EMAIL + r"$",value))
+ def is_email(cls, value):
+ return bool(re.search(r"^" + EMAIL + r"$", value))
@classmethod
- def is_phone(cls,value):
+ def is_phone(cls, value):
return re.search(r"^" + LOCAL + r"$", value) or\
- re.search(r"^" + INTERNATIONAL + r"$",value)
+ re.search(r"^" + INTERNATIONAL + r"$", value)
@classmethod
- def is_hostname(cls,value):
+ def is_hostname(cls, value):
return bool(re.search(r"^" + HOST + r"$", value))
@classmethod
- def is_ip_address(cls,value):
- return bool(re.search(IP_ADDRESS,value))
+ def is_ip_address(cls, value):
+ return bool(re.search(IP_ADDRESS, value))
@classmethod
- def is_number(cls,value):
- return bool(re.search(NUMBER,value))
+ def is_number(cls, value):
+ return bool(re.search(NUMBER, value))
@classmethod
- def is_integer(cls,value):
+ def is_integer(cls, value):
return bool(re.search(INT_NUMBER, value))
@classmethod
- def is_date(cls,value):
- return bool(re.search(r"^" + DATE + r"$",value))
+ def is_date(cls, value):
+ return bool(re.search(r"^" + DATE + r"$", value))
@classmethod
- def is_time(cls,value):
- return bool(re.search(r"^" + HOUR + r"$",value))
+ def is_time(cls, value):
+ return bool(re.search(r"^" + HOUR + r"$", value))
@classmethod
- def is_datetime(cls,value):
- return bool(re.search(DATE_AND_HOUR,value))
+ def is_datetime(cls, value):
+ return bool(re.search(DATE_AND_HOUR, value))

Владимир обнови решението на 23.04.2014 16:50 (преди над 10 години)

import re
HOST_NAME = r"(\w(\w|-){,61}\w\.)+"
HOST_TLD = r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,2})?"
HOST = HOST_NAME + HOST_TLD
EMAIL_ID = r"\w(\w|_|\+|\.|-){,200}"
EMAIL = EMAIL_ID + "@" + HOST
PHONE = r"(\(|\))?\d([ \-\(\)]{,2}\d){4,9}\d"
CODE = r"(\b00|\+)[1-9]\d{,2}"
INTERNATIONAL = CODE + PHONE
LOCAL = r"\b0(?=[^0])" + PHONE
BYTE_NUMBER = \
r"\b((0)|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([1-9]))\b"
IP_ADDRESS = r"^" + BYTE_NUMBER + r'\.' + BYTE_NUMBER + r'\.' +\
BYTE_NUMBER + r'\.' + BYTE_NUMBER + r"$"
INT_NUMBER = r"^-?(0|[1-9][0-9]*)$"
NUMBER = r"^-?(0|[1-9][0-9]*)(\.[0-9]+)?$"
DATE = r"\d\d\d\d-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\d)|(30)|(31))"
HOUR = r"(([0-1]\d)|(2[0-3]))(:[0-5]\d)(:[0-5]\d)"
DATE_AND_HOUR = r"^" + DATE + r" |T" + HOUR + r"$"
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):
if not self.preserve_email_hostname and \
not self.preserve_phone_country_code and \
not self.partially_preserve_email_username:
result = re.sub(EMAIL, "[EMAIL]", self.text)
result = re.sub(INTERNATIONAL, "[PHONE]", result)
result = re.sub(LOCAL, "[PHONE]", result)
return result
result = self.text
-
+
if self.partially_preserve_email_username:
self.preserve_email_hostname = True
while re.search(EMAIL, result):
start, end = re.search(EMAIL, result).span()
end1 = re.search(EMAIL_ID, result[start:end]).end()
if end1 - start >= 6:
result = result[:start+3] +\
"[FILTERED]" +\
result[end1:]
if self.preserve_email_hostname:
while re.search(EMAIL, result):
start, end = re.search(EMAIL, result).span()
result = result[:start] +\
- re.sub(EMAIL_ID, " [FILTERED]", result[start:end]) +\
+ re.sub(EMAIL_ID + r"@", "[FILTERED]@", result[start:end]) +\
result[end:]
if self.preserve_phone_country_code:
while re.search(INTERNATIONAL, result):
start, end = re.search(INTERNATIONAL, result).span()
- result = result[:start] + \
- re.sub(PHONE, " [FILTERED]", result[start:end]) + result[end:]
+ end1 = re.search(CODE, result[start:end]).end()
+ result = result[:end1] +\
+ " [PHONE]" + result[end:]
return result
class Validations:
@classmethod
def is_email(cls, value):
return bool(re.search(r"^" + EMAIL + r"$", value))
@classmethod
def is_phone(cls, value):
return re.search(r"^" + LOCAL + r"$", value) or\
re.search(r"^" + INTERNATIONAL + r"$", value)
@classmethod
def is_hostname(cls, value):
return bool(re.search(r"^" + HOST + r"$", value))
@classmethod
def is_ip_address(cls, value):
return bool(re.search(IP_ADDRESS, value))
@classmethod
def is_number(cls, value):
return bool(re.search(NUMBER, value))
@classmethod
def is_integer(cls, value):
return bool(re.search(INT_NUMBER, value))
@classmethod
def is_date(cls, value):
return bool(re.search(r"^" + DATE + r"$", value))
@classmethod
def is_time(cls, value):
return bool(re.search(r"^" + HOUR + r"$", value))
@classmethod
def is_datetime(cls, value):
return bool(re.search(DATE_AND_HOUR, value))

Владимир обнови решението на 23.04.2014 16:59 (преди над 10 години)

import re
HOST_NAME = r"(\w(\w|-){,61}\w\.)+"
HOST_TLD = r"[a-zA-Z]{2,3}(\.[a-zA-Z]{2,2})?"
HOST = HOST_NAME + HOST_TLD
EMAIL_ID = r"\w(\w|_|\+|\.|-){,200}"
EMAIL = EMAIL_ID + "@" + HOST
PHONE = r"(\(|\))?\d([ \-\(\)]{,2}\d){4,9}\d"
CODE = r"(\b00|\+)[1-9]\d{,2}"
INTERNATIONAL = CODE + PHONE
LOCAL = r"\b0(?=[^0])" + PHONE
BYTE_NUMBER = \
r"\b((0)|(1[0-9][0-9])|(2[0-4][0-9])|(25[0-5])|([1-9][0-9])|([1-9]))\b"
IP_ADDRESS = r"^" + BYTE_NUMBER + r'\.' + BYTE_NUMBER + r'\.' +\
BYTE_NUMBER + r'\.' + BYTE_NUMBER + r"$"
INT_NUMBER = r"^-?(0|[1-9][0-9]*)$"
NUMBER = r"^-?(0|[1-9][0-9]*)(\.[0-9]+)?$"
DATE = r"\d\d\d\d-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\d)|(30)|(31))"
HOUR = r"(([0-1]\d)|(2[0-3]))(:[0-5]\d)(:[0-5]\d)"
DATE_AND_HOUR = r"^" + DATE + r" |T" + HOUR + r"$"
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):
if not self.preserve_email_hostname and \
not self.preserve_phone_country_code and \
not self.partially_preserve_email_username:
result = re.sub(EMAIL, "[EMAIL]", self.text)
result = re.sub(INTERNATIONAL, "[PHONE]", result)
result = re.sub(LOCAL, "[PHONE]", result)
return result
result = self.text
if self.partially_preserve_email_username:
self.preserve_email_hostname = True
while re.search(EMAIL, result):
start, end = re.search(EMAIL, result).span()
end1 = re.search(EMAIL_ID, result[start:end]).end()
if end1 - start >= 6:
result = result[:start+3] +\
"[FILTERED]" +\
result[end1:]
if self.preserve_email_hostname:
while re.search(EMAIL, result):
start, end = re.search(EMAIL, result).span()
result = result[:start] +\
re.sub(EMAIL_ID + r"@", "[FILTERED]@", result[start:end]) +\
result[end:]
if self.preserve_phone_country_code:
while re.search(INTERNATIONAL, result):
start, end = re.search(INTERNATIONAL, result).span()
- end1 = re.search(CODE, result[start:end]).end()
- result = result[:end1] +\
- " [PHONE]" + result[end:]
+ start1, end1 = re.search(CODE, result[start:end]).span()
+ result = result[:end1+start] +\
+ " [FILTERED]" + result[end:]
return result
class Validations:
@classmethod
def is_email(cls, value):
return bool(re.search(r"^" + EMAIL + r"$", value))
@classmethod
def is_phone(cls, value):
return re.search(r"^" + LOCAL + r"$", value) or\
re.search(r"^" + INTERNATIONAL + r"$", value)
@classmethod
def is_hostname(cls, value):
return bool(re.search(r"^" + HOST + r"$", value))
@classmethod
def is_ip_address(cls, value):
return bool(re.search(IP_ADDRESS, value))
@classmethod
def is_number(cls, value):
return bool(re.search(NUMBER, value))
@classmethod
def is_integer(cls, value):
return bool(re.search(INT_NUMBER, value))
@classmethod
def is_date(cls, value):
return bool(re.search(r"^" + DATE + r"$", value))
@classmethod
def is_time(cls, value):
return bool(re.search(r"^" + HOUR + r"$", value))
@classmethod
def is_datetime(cls, value):
return bool(re.search(DATE_AND_HOUR, value))