Решение на Регулярни изрази от Ангел Цанев

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

Към профила на Ангел Цанев

Резултати

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

Код

import re
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):
to_filter = self.text
user = r'((?:\b|\B)[a-zA-Z0-9][\w\+\.\-]{,200}@)'
hostname = r'(((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$)'
email = user + hostname
if self.preserve_email_hostname:
to_filter = re.sub(email, r'[FILTERED]@\2', to_filter)
elif self.partially_preserve_email_username:
to_filter = re.sub(
r'((?:\b)[a-zA-Z0-9])[\w\+\.\-]{,4}@'+hostname,
r'[FILTERED]@\2',
to_filter)
to_filter = re.sub(
r'((?:\b|\B)[a-zA-Z0-9][\w\+\.\-]{2})[\w\+\.\-]{3,198}@'+hostname,
r'\1[FILTERED]@\2',
to_filter)
else:
to_filter = re.sub(email, r'[EMAIL]', to_filter)
phone = r'(?:\b|\B)(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}$'
if self.preserve_phone_country_code:
to_filter = re.sub(phone, r'\1\2 [FILTERED]', to_filter)
else:
to_filter = re.sub(phone, r'[PHONE]', to_filter)
to_filter = re.sub(
r'(?:\b|\B)0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}$',
r'[PHONE]',
to_filter)
return to_filter
class Validations:
@classmethod
def is_email(klas, value):
user = r'^[a-zA-Z0-9][\w\+\.\-]{,200}@'
hostname = r'((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
email = user + hostname
return bool(re.match(email, value))
@classmethod
def is_phone(klas, value):
return klas.is_local_phone(value) or klas.is_international_phone(value)
@classmethod
def is_local_phone(klas, value):
local = r'^0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}$'
return bool(re.match(local, value))
@classmethod
def is_international_phone(klas, value):
international = r'^(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}$'
return bool(re.match(international, value))
@classmethod
def is_hostname(klas, value):
hostname = r'^((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
return bool(re.match(hostname, value))
@classmethod
def is_ip_address(klas, value):
ipv4 = r'^(([0-2][0-5]{2}|[1]{,1}\d{1,2})\.){3}([0-2][0-5]{2}|[1]{,1}\d{1,2})$'
return bool(re.match(ipv4, value))
@classmethod
def is_number(klas, value):
number = r'^\-?(0|([1-9]\d*))(\.\d+)?$'
return bool(re.match(number, value))
@classmethod
def is_integer(klas, value):
integer = r'^\-?(0|([1-9]\d*))$'
return bool(re.match(integer, value))
@classmethod
def is_date(klas, value):
date = r'^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|(1|2)\d|3[0-1])$'
return bool(re.match(date, value))
@classmethod
def is_time(klas, value):
time = r'^((0|1)\d|2[0-3]):[0-5]\d:[0-5]\d$'
return bool(re.match(time, value))
@classmethod
def is_datetime(klas, value):
date = r'^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|(1|2)\d|3[0-1])'
time = r'((0|1)\d|2[0-3]):[0-5]\d:[0-5]\d$'
datetime = date + '(\ |T)' + time
return bool(re.match(datetime, value))

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

...FF..F.F......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-vp24yn/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-vp24yn/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_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-vp24yn/test.py", line 37, in test_obfuscates_more_complicated_emails
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: 'Contact: [EMAIL],[EMAIL]' != 'Contact: 1someone@example.com,[EMAIL]'
- Contact: [EMAIL],[EMAIL]
+ Contact: 1someone@example.com,[EMAIL]


======================================================================
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-vp24yn/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...' != ' +359881212-12-1 2 or...'
-  [PHONE] or...
+  +359881212-12-1 2 or...


======================================================================
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-vp24yn/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-vp24yn/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-vp24yn/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-vp24yn/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-vp24yn/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.055s

FAILED (failures=9)

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

Ангел обнови решението на 22.04.2014 19:01 (преди над 10 години)

+import re
+
+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):
+ to_filter = self.text
+
+ user = r'(\b[a-zA-Z0-9][\w\+\.\-]{,200}@)'
+ hostname = r'(((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)'
+ email = user + hostname
+
+ if self.preserve_email_hostname:
+ to_filter = re.sub(email, r'[FILTERED]@\2', to_filter)
+ elif self.partially_preserve_email_username:
+ to_filter = re.sub(r'(\b[a-zA-Z0-9])[\w\+\.\-]{,4}@'+hostname, r'[FILTERED]@\2', to_filter)
+ to_filter = re.sub(r'(\b[a-zA-Z0-9][\w\+\.\-]{2})[\w\+\.\-]{3,198}@'+hostname, r'\1[FILTERED]@\2', to_filter)
+ else:
+ to_filter = re.sub(email, r'[EMAIL]',to_filter)
+
+ if self.preserve_phone_country_code:
+ to_filter = re.sub(r'(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}', r'\1\2 [FILTERED]', to_filter)
+ else:
+ to_filter = re.sub(r'(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}', r'[PHONE]', to_filter)
+ to_filter = re.sub(r'0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}', r'[PHONE]', to_filter)
+
+ return to_filter
+
+class Validations:
+ @classmethod
+ def is_email(klas, value):
+ user = r'^[a-zA-Z0-9][\w\+\.\-]{,200}@'
+ hostname = r'((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
+ email = user + hostname
+ return bool(re.match(email, value))
+
+ @classmethod
+ def is_phone(klas, value):
+ return klas.is_local_phone(value) or klas.is_international_phone(value)
+
+ @classmethod
+ def is_local_phone(klas, value):
+ local = r'^0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}$'
+ return bool(re.match(local, value))
+
+ @classmethod
+ def is_international_phone(klas, value):
+ international = r'^(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}$'
+ return bool(re.match(international, value))
+
+ @classmethod
+ def is_hostname(klas, value):
+ hostname = r'^((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
+ return bool(re.match(hostname, value))
+
+ @classmethod
+ def is_ip_address(klas, value):
+ ipv4 = r'^(([0-2][0-5]{2}|[1]{,1}\d{1,2})\.){3}([0-2][0-5]{2}|[1]{,1}\d{1,2})$'
+ return bool(re.match(ipv4, value))
+
+ @classmethod
+ def is_number(klas, value):
+ number = r'^\-?(0|([1-9]\d*))(\.\d+)?$'
+ return bool(re.match(number, value))
+
+ @classmethod
+ def is_integer(klas, value):
+ integer = r'^\-?(0|([1-9]\d*))$'
+ return bool(re.match(integer, value))
+
+ @classmethod
+ def is_date(klas, value):
+ date = r'^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|(1|2)\d|3[0-1])$'
+ return bool(re.match(date, value))
+
+ @classmethod
+ def is_time(klas, value):
+ time = r'^((0|1)\d|2[0-3]):[0-5]\d:[0-5]\d$'
+ return bool(re.match(time, value))
+
+ @classmethod
+ def is_datetime(klas, value):
+ date = r'^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|(1|2)\d|3[0-1])'
+ time = r'((0|1)\d|2[0-3]):[0-5]\d:[0-5]\d$'
+ datetime = date + '(\ |T)' + time
+ return bool(re.match(datetime, value))

Ангел обнови решението на 22.04.2014 21:05 (преди над 10 години)

import re
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):
to_filter = self.text
- user = r'(\b[a-zA-Z0-9][\w\+\.\-]{,200}@)'
- hostname = r'(((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?)'
+ user = r'((?:\b|\B)[a-zA-Z0-9][\w\+\.\-]{,200}@)'
+ hostname = r'(((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$)'
email = user + hostname
if self.preserve_email_hostname:
to_filter = re.sub(email, r'[FILTERED]@\2', to_filter)
elif self.partially_preserve_email_username:
- to_filter = re.sub(r'(\b[a-zA-Z0-9])[\w\+\.\-]{,4}@'+hostname, r'[FILTERED]@\2', to_filter)
- to_filter = re.sub(r'(\b[a-zA-Z0-9][\w\+\.\-]{2})[\w\+\.\-]{3,198}@'+hostname, r'\1[FILTERED]@\2', to_filter)
+ to_filter = re.sub(
+ r'((?:\b)[a-zA-Z0-9])[\w\+\.\-]{,4}@'+hostname,
+ r'[FILTERED]@\2',
+ to_filter)
+
+ to_filter = re.sub(
+ r'((?:\b|\B)[a-zA-Z0-9][\w\+\.\-]{2})[\w\+\.\-]{3,198}@'+hostname,
+ r'\1[FILTERED]@\2',
+ to_filter)
else:
- to_filter = re.sub(email, r'[EMAIL]',to_filter)
+ to_filter = re.sub(email, r'[EMAIL]', to_filter)
+ phone = r'(?:\b|\B)(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}$'
+
if self.preserve_phone_country_code:
- to_filter = re.sub(r'(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}', r'\1\2 [FILTERED]', to_filter)
+ to_filter = re.sub(phone, r'\1\2 [FILTERED]', to_filter)
else:
- to_filter = re.sub(r'(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}', r'[PHONE]', to_filter)
- to_filter = re.sub(r'0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}', r'[PHONE]', to_filter)
+ to_filter = re.sub(phone, r'[PHONE]', to_filter)
+ to_filter = re.sub(
+ r'(?:\b|\B)0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}$',
+ r'[PHONE]',
+ to_filter)
return to_filter
+
class Validations:
@classmethod
def is_email(klas, value):
user = r'^[a-zA-Z0-9][\w\+\.\-]{,200}@'
hostname = r'((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
email = user + hostname
return bool(re.match(email, value))
@classmethod
def is_phone(klas, value):
return klas.is_local_phone(value) or klas.is_international_phone(value)
@classmethod
def is_local_phone(klas, value):
local = r'^0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}$'
return bool(re.match(local, value))
@classmethod
def is_international_phone(klas, value):
international = r'^(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}$'
return bool(re.match(international, value))
@classmethod
def is_hostname(klas, value):
hostname = r'^((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
return bool(re.match(hostname, value))
@classmethod
def is_ip_address(klas, value):
ipv4 = r'^(([0-2][0-5]{2}|[1]{,1}\d{1,2})\.){3}([0-2][0-5]{2}|[1]{,1}\d{1,2})$'
return bool(re.match(ipv4, value))
@classmethod
def is_number(klas, value):
number = r'^\-?(0|([1-9]\d*))(\.\d+)?$'
return bool(re.match(number, value))
@classmethod
def is_integer(klas, value):
integer = r'^\-?(0|([1-9]\d*))$'
return bool(re.match(integer, value))
@classmethod
def is_date(klas, value):
date = r'^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|(1|2)\d|3[0-1])$'
return bool(re.match(date, value))
@classmethod
def is_time(klas, value):
time = r'^((0|1)\d|2[0-3]):[0-5]\d:[0-5]\d$'
return bool(re.match(time, value))
@classmethod
def is_datetime(klas, value):
date = r'^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|(1|2)\d|3[0-1])'
time = r'((0|1)\d|2[0-3]):[0-5]\d:[0-5]\d$'
datetime = date + '(\ |T)' + time
return bool(re.match(datetime, value))

Ангел обнови решението на 22.04.2014 21:09 (преди над 10 години)

import re
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):
to_filter = self.text
user = r'((?:\b|\B)[a-zA-Z0-9][\w\+\.\-]{,200}@)'
hostname = r'(((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$)'
email = user + hostname
if self.preserve_email_hostname:
to_filter = re.sub(email, r'[FILTERED]@\2', to_filter)
elif self.partially_preserve_email_username:
to_filter = re.sub(
- r'((?:\b)[a-zA-Z0-9])[\w\+\.\-]{,4}@'+hostname,
- r'[FILTERED]@\2',
- to_filter)
+ r'((?:\b)[a-zA-Z0-9])[\w\+\.\-]{,4}@'+hostname,
+ r'[FILTERED]@\2',
+ to_filter)
to_filter = re.sub(
- r'((?:\b|\B)[a-zA-Z0-9][\w\+\.\-]{2})[\w\+\.\-]{3,198}@'+hostname,
- r'\1[FILTERED]@\2',
- to_filter)
+ r'((?:\b|\B)[a-zA-Z0-9][\w\+\.\-]{2})[\w\+\.\-]{3,198}@'+hostname,
+ r'\1[FILTERED]@\2',
+ to_filter)
else:
to_filter = re.sub(email, r'[EMAIL]', to_filter)
phone = r'(?:\b|\B)(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}$'
if self.preserve_phone_country_code:
to_filter = re.sub(phone, r'\1\2 [FILTERED]', to_filter)
else:
to_filter = re.sub(phone, r'[PHONE]', to_filter)
to_filter = re.sub(
- r'(?:\b|\B)0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}$',
- r'[PHONE]',
- to_filter)
+ r'(?:\b|\B)0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}$',
+ r'[PHONE]',
+ to_filter)
return to_filter
class Validations:
@classmethod
def is_email(klas, value):
user = r'^[a-zA-Z0-9][\w\+\.\-]{,200}@'
hostname = r'((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
email = user + hostname
return bool(re.match(email, value))
@classmethod
def is_phone(klas, value):
return klas.is_local_phone(value) or klas.is_international_phone(value)
@classmethod
def is_local_phone(klas, value):
local = r'^0(([\-\ \(\)]{1,2}[0-9])|([1-9]))([\-\ \(\)]{,2}[0-9]){5,10}$'
return bool(re.match(local, value))
@classmethod
def is_international_phone(klas, value):
international = r'^(00|\+)([1-9]{1}[0-9]{,2})([\-\ \(\)]{,2}[0-9]){6,11}$'
return bool(re.match(international, value))
@classmethod
def is_hostname(klas, value):
hostname = r'^((([0-9a-zA-Z])|([0-9a-zA-Z][0-9a-zA-Z\-]{,61}[0-9a-zA-Z]))\.)+[a-zA-Z]{2,3}(\.[a-zA-Z]{2,3})?$'
return bool(re.match(hostname, value))
@classmethod
def is_ip_address(klas, value):
ipv4 = r'^(([0-2][0-5]{2}|[1]{,1}\d{1,2})\.){3}([0-2][0-5]{2}|[1]{,1}\d{1,2})$'
return bool(re.match(ipv4, value))
@classmethod
def is_number(klas, value):
number = r'^\-?(0|([1-9]\d*))(\.\d+)?$'
return bool(re.match(number, value))
@classmethod
def is_integer(klas, value):
integer = r'^\-?(0|([1-9]\d*))$'
return bool(re.match(integer, value))
@classmethod
def is_date(klas, value):
date = r'^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|(1|2)\d|3[0-1])$'
return bool(re.match(date, value))
@classmethod
def is_time(klas, value):
time = r'^((0|1)\d|2[0-3]):[0-5]\d:[0-5]\d$'
return bool(re.match(time, value))
@classmethod
def is_datetime(klas, value):
date = r'^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|(1|2)\d|3[0-1])'
time = r'((0|1)\d|2[0-3]):[0-5]\d:[0-5]\d$'
datetime = date + '(\ |T)' + time
return bool(re.match(datetime, value))