import re import unittest import logging pattern_char_only = re.compile('^[A-Z][a-z]+$') pattern_plz = re.compile('^[0-9]{5}$') pattern_plz2 = re.compile('^\d{5}$') pattern_quote= re.compile(':') pattern_tab = re.compile(r'\t') pattern_simpson = re.compile('Simpson') logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG) class TestRegEgEx(unittest.TestCase): def test_match(self): m = pattern_char_only.match('Olli') logging.debug(f'm={m}') self.assertIsNotNone(m) def test_findall(self): m = pattern_char_only.findall('Olli') logging.debug(f'm={m}') self.assertIsNotNone(m) def test_plz(self): patterns = [pattern_plz,pattern_plz2] plz = ['42275','5600'] for pattern in patterns: m = pattern.findall(plz[0]) logging.debug(f'm={m}') self.assertNotEqual('',m[0]) m = pattern.findall(plz[1]) logging.debug(f'm={m}') self.assertEqual([],m) def test_search(self): m = pattern_char_only.search('Olli') logging.debug(f'm={m}') self.assertIsNotNone(m) m = pattern_char_only.search('123456') logging.debug(f'm={m}') self.assertIsNone(m) def test_split(self): m = pattern_quote.split('Die:Würde:des:Menschen:ist:unantastbar.') logging.debug(f'm={m}') self.assertIsNotNone(m) def test_sub(self): m = pattern_tab.sub(' ','Satz\tmit\tTabulator.') logging.debug(f'm={m}') def test_finditer(self): m = pattern_simpson.finditer('Homer Simpson, Marge Simpson, Bart Simpson, Lisa Simpson, Maggie Simpson') logging.debug(f'm={m}') for name in m: logging.debug(f'Name={name}') if __name__ == '__main__': unittest.main()