├── test_regex.py └── regex.py /test_regex.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | import regex 3 | 4 | 5 | class TestRegex(unittest.TestCase): # a test case for the regex.py module 6 | samples_for_true = {1: ['colou?r', 'color'], 7 | 2: ['colou?r', 'colour'], 8 | 3: ['colou*r', 'color'], 9 | 4: ['colou*r', 'colour'], 10 | 5: ['colou*r', 'colouur'], 11 | 6: ['col.*r', 'color'], 12 | 7: ['col.*r', 'colour'], 13 | 8: ['col.*r', 'colr'], 14 | 9: ['col.*r', 'collar'], 15 | 10: ['colou+r', 'colour'], 16 | 11: ['^no+pe$', 'noooooooope'], 17 | 12: ['^n.+pe$', 'noooooooope'], 18 | 13: ['.*', 'aaa'], 19 | 14: ['\\.$', 'end.'], 20 | 15: ['3\\+3', '3+3=6'], 21 | 16: ['\\?', 'Is this working?'], 22 | 17: ['\\\\', '\\']} 23 | 24 | samples_for_false = {1: ['colou?r', 'colouur'], 25 | 2: ['col.*r$', 'colors'], 26 | 3: ['app$', 'apple'], 27 | 4: ['^le', 'apple'], 28 | 5: ['colou+r', 'color'], 29 | 6: ['^n.+p$', 'noooooooope'], 30 | 7: ['colou\\?r', 'color'], 31 | 8: ['colou\\?r', 'colour']} 32 | 33 | def test_regex_str_cmp_true(self): 34 | # tests for the regex_str_cmp() function 35 | for key, val in self.samples_for_true.items(): 36 | self.assertEqual(regex.regex_str_cmp(val[0], val[1]), True, f'{key}: {val[0]} - {val[1]}') 37 | 38 | def test_regex_str_cmp_false(self): 39 | # tests for the regex_str_cmp() function 40 | for key, val in self.samples_for_false.items(): 41 | self.assertEqual(regex.regex_str_cmp(val[0], val[1]), False, f'{key}: {val[0]} - {val[1]}') 42 | 43 | 44 | if __name__ == '__main__': 45 | unittest.main() 46 | -------------------------------------------------------------------------------- /regex.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.setrecursionlimit(10000) 3 | 4 | 5 | def regex_chr_cmp(rgx_, chr_): 6 | if bool((rgx_ == chr_) or (not rgx_ or rgx_ == '.')): 7 | return True 8 | return False 9 | 10 | 11 | def regex_back_check(rgx_, default_str_): 12 | for i in reversed(range(len(default_str_))): 13 | # print(default_str_[i:]) 14 | if regex_eq_str_cmp(rgx_, default_str_[i:], default_str_): 15 | if len(rgx_) == len(default_str_[i:]): 16 | return True 17 | return False 18 | 19 | 20 | def regex_eq_str_cmp(rgx_, str_, default_str_, count=0): 21 | if rgx_ == '$' and not str_: 22 | return True 23 | elif not rgx_: 24 | return True 25 | else: 26 | if rgx_[0] == '\\': 27 | rgx_ = rgx_[1:] 28 | 29 | if not str_: 30 | if count == 0 and regex_back_check(rgx_[2:], default_str_): 31 | return True 32 | if count > 0 and regex_back_check(rgx_[:-1], default_str_): 33 | return True 34 | return False 35 | else: 36 | if len(rgx_) > 1 and rgx_[1] == '?' and rgx_[0] != '\\': 37 | if regex_chr_cmp(rgx_[0], str_[0]): 38 | return regex_eq_str_cmp(rgx_[2:], str_[1:], default_str_) 39 | else: 40 | return regex_eq_str_cmp(rgx_[2:], str_, default_str_) 41 | elif len(rgx_) > 1 and rgx_[1] == '*' and rgx_[0] != '\\': 42 | if len(str_) > 1 and regex_chr_cmp(rgx_[0], str_[0]): 43 | return regex_eq_str_cmp(rgx_, str_[1:], default_str_, count+1) 44 | else: 45 | return regex_eq_str_cmp(rgx_[2:], str_, default_str_, count+1) 46 | elif len(rgx_) > 1 and rgx_[1] == '+' and rgx_[0] != '\\': 47 | if regex_chr_cmp(rgx_[0], str_[0]): 48 | if len(str_) > 1 and regex_chr_cmp(rgx_[0], str_[1]): 49 | return regex_eq_str_cmp(rgx_, str_[1:], default_str_, count+1) 50 | else: 51 | return regex_eq_str_cmp(rgx_[2:], str_[1:], default_str_, count+1) 52 | else: 53 | return False 54 | elif not regex_chr_cmp(rgx_[0], str_[0]): 55 | return False 56 | else: 57 | return regex_eq_str_cmp(rgx_[1:], str_[1:], default_str_) 58 | 59 | 60 | def regex_str_cmp(rgx_, str_): 61 | default_str = str_ 62 | if regex_eq_str_cmp(rgx_, str_, default_str): 63 | return True 64 | elif rgx_[0] == '^': 65 | return regex_eq_str_cmp(rgx_[1:], str_, default_str) 66 | else: 67 | if not str_: 68 | return False 69 | else: 70 | return regex_str_cmp(rgx_, str_[1:]) 71 | 72 | 73 | if __name__ == '__main__': 74 | regex, str_input = input().split('|') 75 | print(regex_str_cmp(regex, str_input)) 76 | 77 | --------------------------------------------------------------------------------