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

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

Към профила на Калоян Кацаров

Резултати

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

Код

import re
class Patterns:
host = ("((([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]{1})|"
"([a-zA-Z0-9]{1}))"
"((\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]{1}){0,}|"
"(\.[a-zA-Z0-9]{1}){0,})"
"(\.[a-zA-Z]{2,3})(\.[a-zA-Z]{2,3}){0,1})")
email = "(([a-zA-Z0-9]{1}[a-zA-Z0-9_+.-]{0,200})@" + host + ")"
phone = ("^((0[ \-()]{0,1}[123456789]{1}[ \-()]{0,1}"
"([0-9]{1}[ \-()]{0,1}){4,9}"
"[0-9]{1})|"
"((00)|\+)[123456789]{1,3}[ \-()]{0,1}[0-9]{1}[ \-()]{0,1}"
"([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})$")
national_rest_of_phone = ("^([0-9]{1}[ \-()]{0,1}"
"([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1}$")
national_phone = ("(((00)|\+)[123456789]{1,3}[ \-()]{0,1}[0-9]{1}"
"[ \-()]{0,1}([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})")
country_phone = ("(0[ \-()]{0,1}[123456789]{1}[ \-()]{0,1}"
"([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})")
county_phone_code = "(((00)|\+)[123456789]{1,3})"
hostname = "[a-zA-Z0-9]{1}[a-zA-Z0-9_+.-]{0,200}"
ip = ("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
"([ (\[]?(\.|dot)[ )\]]?(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})$")
number = "^[\-]{0,1}(0|[123456789]{1})[0-9]{0,}(\.[0-9]{1,}){0,1}$"
integer = "^[\-]{0,1}(0|[123456789]{1})[0-9]{0,}$"
date = ("(((19|20)([2468][048]|[13579][26]|0[48])|2000)"
"[\-]02[\-]29|((19|20)[0-9]{2}[\-](0[4678]|1[02])"
"[\-](0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}[\-](0[1359]|11)"
"[\-](0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}[\-]02[\-]"
"(0[1-9]|1[0-9]|2[0-8])))")
time = "(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)"
date_time = date + "[ T]" + time
class PrivacyFilter:
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
phone = '[PHONE]'
email_string = "[EMAIL]"
filtered_string = "[FILTERED]"
def __init__(self, text):
self.text = text
self.preserve_phone_country_code
def filtered(self):
if self.preserve_email_hostname == True:
match_object = re.search(Patterns.email , self.text,re.MULTILINE)
if match_object != None:
for match in match_object.groups():
if match != None and re.match("^" + Patterns.email + "$",
match + "") != None:
match_object = re.search(("("+
Patterns.hostname+"@"+
")"), match)
if match_object != None:
for match in match_object.groups():
tmp= self.filtered_string + "@"
self.text = self.text.replace(match,tmp)
if self.partially_preserve_email_username == True:
match_object = re.search(Patterns.email , self.text,re.MULTILINE)
if match_object != None:
for match in match_object.groups():
if match != None and re.match("^" + Patterns.email + "$",
match + "") != None:
host_plus_at = re.match("(" +
Patterns.hostname + "@" + ")",
match + "").group(1)
if host_plus_at.__len__() >= 7:
prefix_of_mail = (host_plus_at[:3] +
self.filtered_string + "@")
tmp = match.replace(host_plus_at, prefix_of_mail)
self.text = self.text.replace(match, tmp)
else:
prefix_of_mail = self.filtered_string + "@"
tmp = match.replace(host_plus_at, prefix_of_mail)
self.text = self.text.replace(match, tmp)
self.text = re.sub(Patterns.email, self.email_string , self.text)
self.text = re.sub(Patterns.country_phone, self.phone, self.text)
if self.preserve_phone_country_code == True:
match_object = re.search(Patterns.national_phone , self.text,
re.MULTILINE)
if match_object != None:
for match in match_object.groups():
if match != None and re.match(("^" +
Patterns.national_phone+
"$"), match + "") != None:
tmp = (re.search(Patterns.county_phone_code,
match).group(1)
+ " " + self.filtered_string)
self.text = self.text.replace(match, tmp)
return self.text
class Validations:
@staticmethod
def is_phone(phone):
if re.match(Patterns.phone, phone) != None:
return True
else:
return False
@staticmethod
def is_email(email):
if re.match("^" + Patterns.email + "$" , email) != None:
return True
else:
return False
@staticmethod
def is_hostname(hostname):
if re.match("^" + Patterns.host + "$", hostname) != None:
return True
else:
return False
@staticmethod
def is_number(number):
if re.match(Patterns.number, number) != None:
return True
else:
return False
@staticmethod
def is_integer(integer):
if re.match(Patterns.integer, integer) != None:
return True
else:
return False
@staticmethod
def is_time(time):
if re.match("^" + Patterns.time + "$", time) != None:
return True
else:
return False
@staticmethod
def is_ip_address(ip):
if re.match(Patterns.ip, ip) != None:
return True
else:
return False
@staticmethod
def is_date(date):
if re.match("^" + Patterns.date + "$", date) != None:
return True
else:
return False
@staticmethod
def is_datetime(date_time):
if re.match("^" + Patterns.date_time + "$", date_time) != None:
return True
else:
return False

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

...FFF...FF.F.F.FF....FF..FF...F..FF..F
======================================================================
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-1hh5smu/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-1hh5smu/test.py", line 86, in test_does_not_filter_invalid_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '0005551234569' != '00[PHONE]'
- 0005551234569
+ 00[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-1hh5smu/test.py", line 76, in test_filters_more_complex_phone_numbers
    self.assertEqual(filtered, solution.PrivacyFilter(text).filtered())
AssertionError: '[PHONE]' != '0[PHONE]'
- [PHONE]
+ 0[PHONE]
? +


======================================================================
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-1hh5smu/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_separates_preserved_country_code_from_filtered_phone_with_a_space (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-1hh5smu/test.py", line 100, in test_separates_preserved_country_code_from_filtered_phone_with_a_space
    self.assertEqual(filtered, filter.filtered())
AssertionError: 'Phone: 0025 [FILTERED]' != 'Phone: 0025(55) 12 12255'
- Phone: 0025 [FILTERED]
+ Phone: 0025(55) 12 12255


======================================================================
FAIL: test_allows_huge_years_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-1hh5smu/test.py", line 246, in test_allows_huge_years_in_date_validation
    self.assertTrue(solution.Validations.is_date('9999-01-01'))
AssertionError: False is not true

======================================================================
FAIL: test_allows_zero_years_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-1hh5smu/test.py", line 243, in test_allows_zero_years_in_date_validation
    self.assertTrue(solution.Validations.is_date('0000-01-01'))
AssertionError: False is not true

======================================================================
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-1hh5smu/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_does_not_allow_invalid_hours_minutes_or_seconds (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-1hh5smu/test.py", line 273, in test_does_not_allow_invalid_hours_minutes_or_seconds
    self.assertFalse(solution.Validations.is_time('12:1:9'))
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-1hh5smu/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-1hh5smu/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-1hh5smu/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-1hh5smu/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-1hh5smu/test.py", line 280, in test_validates_datetime_values
    self.assertTrue(solution.Validations.is_datetime('9999-11-19T23:59:00'))
AssertionError: False is not true

======================================================================
FAIL: test_validates_more_complex_integers (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-1hh5smu/test.py", line 225, in test_validates_more_complex_integers
    self.assertFalse(solution.Validations.is_integer('00'))
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-1hh5smu/test.py", line 205, in test_validates_more_complex_numbers
    self.assertFalse(solution.Validations.is_number('00'))
AssertionError: True is not false

======================================================================
FAIL: test_validates_times (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-1hh5smu/test.py", line 267, in test_validates_times
    self.assertFalse(solution.Validations.is_time('3:59:59'))
AssertionError: True is not false

----------------------------------------------------------------------
Ran 39 tests in 0.088s

FAILED (failures=17)

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

Калоян обнови решението на 22.04.2014 13:17 (преди над 10 години)

+import re
+
+
+class Patterns:
+ host = ("((([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]{1})|"
+ "([a-zA-Z0-9]{1}))"
+ "((\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]{1}){0,}|"
+ "(\.[a-zA-Z0-9]{1}){0,})"
+ "(\.[a-zA-Z]{2,3})(\.[a-zA-Z]{2,3}){0,1})")
+
+ email = "(([a-zA-Z0-9]{1}[a-zA-Z0-9_+.-]{0,200})@" + host + ")"
+
+ phone = ("^((0[ \-()]{0,1}[123456789]{1}[ \-()]{0,1}"
+ "([0-9]{1}[ \-()]{0,1}){4,9}"
+ "[0-9]{1})|"
+ "((00)|\+)[123456789]{1,3}[ \-()]{0,1}[0-9]{1}[ \-()]{0,1}"
+ "([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})$")
+
+ national_rest_of_phone = ("^([0-9]{1}[ \-()]{0,1}"
+ "([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1}$")
+
+ national_phone = ("(((00)|\+)[123456789]{1,3}[ \-()]{0,1}[0-9]{1}"
+ "[ \-()]{0,1}([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})")
+
+ country_phone = ("(0[ \-()]{0,1}[123456789]{1}[ \-()]{0,1}"
+ "([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})")
+
+ county_phone_code = "(((00)|\+)[123456789]{1,3})"
+
+ hostname = "[a-zA-Z0-9]{1}[a-zA-Z0-9_+.-]{0,200}"
+
+ ip = ("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
+ "([ (\[]?(\.|dot)[ )\]]?(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})$")
+
+ number = "^[\-]{0,1}(0|[123456789]{1})[0-9]{0,}(\.[0-9]{1,}){0,1}$"
+
+ integer = "^[\-]{0,1}(0|[123456789]{1})[0-9]{0,}$"
+
+ date = ("(((19|20)([2468][048]|[13579][26]|0[48])|2000)"
+ "[\-]02[\-]29|((19|20)[0-9]{2}[\-](0[4678]|1[02])"
+ "[\-](0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}[\-](0[1359]|11)"
+ "[\-](0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}[\-]02[\-]"
+ "(0[1-9]|1[0-9]|2[0-8])))")
+
+ time = "(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)"
+
+ date_time = date + "[ T]" + time
+
+class PrivacyFilter:
+
+ preserve_phone_country_code = False
+
+ preserve_email_hostname = False
+
+ partially_preserve_email_username = False
+
+ phone = '[PHONE]'
+
+ email_string = "[EMAIL]"
+
+ filtered_string = "[FILTERED]"
+
+ def __init__(self, text):
+ self.text = text
+ self.preserve_phone_country_code
+
+ def filtered(self):
+ if self.preserve_email_hostname == True:
+ match_object = re.search(Patterns.email , self.text)
+ if match_object != None:
+ for match in match_object.groups():
+ if match != None and re.match("^" + Patterns.email + "$",
+ match + "") != None:
+ match_object = re.search(("("+
+ Patterns.hostname+"@"+
+ ")"), match)
+ if match_object != None:
+ for match in match_object.groups():
+ tmp= self.filtered_string + "@"
+ self.text = self.text.replace(match,tmp)
+
+ if self.partially_preserve_email_username == True:
+ match_object = re.search(Patterns.email , self.text)
+ if match_object != None:
+ for match in match_object.groups():
+ if match != None and re.match("^" + Patterns.email + "$",
+ match + "") != None:
+ host_plus_at = re.match("(" +
+ Patterns.hostname + "@" + ")",
+ match + "").group(1)
+ if host_plus_at.__len__() >= 7:
+ prefix_of_mail = (host_plus_at[:3] +
+ self.filtered_string + "@")
+ tmp = match.replace(host_plus_at, prefix_of_mail)
+ self.text = self.text.replace(match, tmp)
+ else:
+ prefix_of_mail = self.filtered_string + "@"
+ tmp = match.replace(host_plus_at, prefix_of_mail)
+ self.text = self.text.replace(match, tmp)
+
+ self.text = re.sub(Patterns.email, self.email_string , self.text)
+ self.text = re.sub(Patterns.country_phone, self.phone, self.text)
+
+ if self.preserve_phone_country_code == True:
+ match_object = re.search(Patterns.national_phone , self.text)
+ if match_object != None:
+ for match in match_object.groups():
+ if match != None and re.match(("^" +
+ Patterns.national_phone+
+ "$"), match + "") != None:
+ tmp = (re.search(Patterns.county_phone_code,
+ match).group(1)
+ + " " + self.filtered_string)
+ self.text = self.text.replace(match, tmp)
+ return self.text
+
+class Validations:
+ @staticmethod
+ def is_phone(phone):
+ if re.match(Patterns.phone, phone) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_email(email):
+ if re.match("^" + Patterns.email + "$" , email) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_hostname(hostname):
+ if re.match("^" + Patterns.host + "$", hostname) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_number(number):
+ if re.match(Patterns.number, number) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_integer(integer):
+ if re.match(Patterns.integer, integer) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_time(time):
+ if re.match("^" + Patterns.time + "$", time) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_ip_address(ip):
+ if re.match(Patterns.ip, ip) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_date(date):
+ if re.match("^" + Patterns.date + "$", date) != None:
+ return True
+ else:
+ return False
+
+ @staticmethod
+ def is_datetime(date_time):
+ if re.match("^" + Patterns.date_time + "$", date_time) != None:
+ return True
+ else:
+ return False

Калоян обнови решението на 22.04.2014 14:28 (преди над 10 години)

import re
class Patterns:
host = ("((([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]{1})|"
"([a-zA-Z0-9]{1}))"
"((\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,61}[a-zA-Z0-9]{1}){0,}|"
"(\.[a-zA-Z0-9]{1}){0,})"
"(\.[a-zA-Z]{2,3})(\.[a-zA-Z]{2,3}){0,1})")
email = "(([a-zA-Z0-9]{1}[a-zA-Z0-9_+.-]{0,200})@" + host + ")"
phone = ("^((0[ \-()]{0,1}[123456789]{1}[ \-()]{0,1}"
"([0-9]{1}[ \-()]{0,1}){4,9}"
"[0-9]{1})|"
"((00)|\+)[123456789]{1,3}[ \-()]{0,1}[0-9]{1}[ \-()]{0,1}"
"([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})$")
national_rest_of_phone = ("^([0-9]{1}[ \-()]{0,1}"
"([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1}$")
national_phone = ("(((00)|\+)[123456789]{1,3}[ \-()]{0,1}[0-9]{1}"
"[ \-()]{0,1}([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})")
country_phone = ("(0[ \-()]{0,1}[123456789]{1}[ \-()]{0,1}"
"([0-9]{1}[ \-()]{0,1}){4,9}[0-9]{1})")
county_phone_code = "(((00)|\+)[123456789]{1,3})"
hostname = "[a-zA-Z0-9]{1}[a-zA-Z0-9_+.-]{0,200}"
ip = ("^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
"([ (\[]?(\.|dot)[ )\]]?(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3})$")
number = "^[\-]{0,1}(0|[123456789]{1})[0-9]{0,}(\.[0-9]{1,}){0,1}$"
integer = "^[\-]{0,1}(0|[123456789]{1})[0-9]{0,}$"
date = ("(((19|20)([2468][048]|[13579][26]|0[48])|2000)"
"[\-]02[\-]29|((19|20)[0-9]{2}[\-](0[4678]|1[02])"
"[\-](0[1-9]|[12][0-9]|30)|(19|20)[0-9]{2}[\-](0[1359]|11)"
"[\-](0[1-9]|[12][0-9]|3[01])|(19|20)[0-9]{2}[\-]02[\-]"
"(0[1-9]|1[0-9]|2[0-8])))")
time = "(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)"
date_time = date + "[ T]" + time
class PrivacyFilter:
preserve_phone_country_code = False
preserve_email_hostname = False
partially_preserve_email_username = False
phone = '[PHONE]'
email_string = "[EMAIL]"
filtered_string = "[FILTERED]"
def __init__(self, text):
self.text = text
self.preserve_phone_country_code
def filtered(self):
if self.preserve_email_hostname == True:
- match_object = re.search(Patterns.email , self.text)
+ match_object = re.search(Patterns.email , self.text,re.MULTILINE)
if match_object != None:
for match in match_object.groups():
if match != None and re.match("^" + Patterns.email + "$",
match + "") != None:
match_object = re.search(("("+
Patterns.hostname+"@"+
")"), match)
if match_object != None:
for match in match_object.groups():
tmp= self.filtered_string + "@"
self.text = self.text.replace(match,tmp)
if self.partially_preserve_email_username == True:
- match_object = re.search(Patterns.email , self.text)
+ match_object = re.search(Patterns.email , self.text,re.MULTILINE)
if match_object != None:
for match in match_object.groups():
if match != None and re.match("^" + Patterns.email + "$",
match + "") != None:
host_plus_at = re.match("(" +
Patterns.hostname + "@" + ")",
match + "").group(1)
if host_plus_at.__len__() >= 7:
prefix_of_mail = (host_plus_at[:3] +
self.filtered_string + "@")
tmp = match.replace(host_plus_at, prefix_of_mail)
self.text = self.text.replace(match, tmp)
else:
prefix_of_mail = self.filtered_string + "@"
tmp = match.replace(host_plus_at, prefix_of_mail)
self.text = self.text.replace(match, tmp)
self.text = re.sub(Patterns.email, self.email_string , self.text)
self.text = re.sub(Patterns.country_phone, self.phone, self.text)
if self.preserve_phone_country_code == True:
- match_object = re.search(Patterns.national_phone , self.text)
+ match_object = re.search(Patterns.national_phone , self.text,
+ re.MULTILINE)
if match_object != None:
for match in match_object.groups():
if match != None and re.match(("^" +
Patterns.national_phone+
"$"), match + "") != None:
tmp = (re.search(Patterns.county_phone_code,
match).group(1)
+ " " + self.filtered_string)
self.text = self.text.replace(match, tmp)
return self.text
class Validations:
@staticmethod
def is_phone(phone):
if re.match(Patterns.phone, phone) != None:
return True
else:
return False
@staticmethod
def is_email(email):
if re.match("^" + Patterns.email + "$" , email) != None:
return True
else:
return False
@staticmethod
def is_hostname(hostname):
if re.match("^" + Patterns.host + "$", hostname) != None:
return True
else:
return False
@staticmethod
def is_number(number):
if re.match(Patterns.number, number) != None:
return True
else:
return False
@staticmethod
def is_integer(integer):
if re.match(Patterns.integer, integer) != None:
return True
else:
return False
@staticmethod
def is_time(time):
if re.match("^" + Patterns.time + "$", time) != None:
return True
else:
return False
@staticmethod
def is_ip_address(ip):
if re.match(Patterns.ip, ip) != None:
return True
else:
return False
@staticmethod
def is_date(date):
if re.match("^" + Patterns.date + "$", date) != None:
return True
else:
return False
@staticmethod
def is_datetime(date_time):
if re.match("^" + Patterns.date_time + "$", date_time) != None:
return True
else:
return False