|
@ -3,25 +3,37 @@ import unittest |
|
|
import logging |
|
|
import logging |
|
|
|
|
|
|
|
|
pattern_char_only = re.compile('^[A-Z][a-z]+$') |
|
|
pattern_char_only = re.compile('^[A-Z][a-z]+$') |
|
|
|
|
|
pattern_digits_only = re.compile(r'\d+') |
|
|
|
|
|
pattern_no_digits = re.compile(r'\D+') |
|
|
pattern_plz = re.compile('^[0-9]{5}$') |
|
|
pattern_plz = re.compile('^[0-9]{5}$') |
|
|
pattern_plz2 = re.compile('^\d{5}$') |
|
|
pattern_plz2 = re.compile('^\d{5}$') |
|
|
pattern_quote= re.compile(':') |
|
|
pattern_quote= re.compile(':') |
|
|
pattern_tab = re.compile(r'\t') |
|
|
pattern_tab = re.compile(r'\t') |
|
|
pattern_simpson = re.compile('Simpson') |
|
|
pattern_simpson = re.compile('Simpson') |
|
|
|
|
|
pattern_simple = re.compile('O*') |
|
|
|
|
|
|
|
|
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG) |
|
|
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG) |
|
|
|
|
|
|
|
|
|
|
|
# Unittest für die Demonstration von regular expressions (re) |
|
|
class TestRegEgEx(unittest.TestCase): |
|
|
class TestRegEgEx(unittest.TestCase): |
|
|
|
|
|
|
|
|
|
|
|
# match: Passt der String auf die re |
|
|
def test_match(self): |
|
|
def test_match(self): |
|
|
m = pattern_char_only.match('Olli') |
|
|
m = pattern_char_only.match('Olli') |
|
|
|
|
|
|
|
|
logging.debug(f'm={m}') |
|
|
logging.debug(f'm={m}') |
|
|
self.assertIsNotNone(m) |
|
|
self.assertTrue(m.span()[1] >0) |
|
|
|
|
|
|
|
|
|
|
|
# Sucht alle Vorkommnisse der re im String |
|
|
def test_findall(self): |
|
|
def test_findall(self): |
|
|
|
|
|
s = '5 Äpfel, 10 Bananen, 42 Erdbeeren' |
|
|
|
|
|
logging.debug(f' nur Ziffern: {pattern_digits_only.findall(s)}') |
|
|
|
|
|
logging.debug(f' keine Ziffern: {pattern_no_digits.findall(s)}') |
|
|
m = pattern_char_only.findall('Olli') |
|
|
m = pattern_char_only.findall('Olli') |
|
|
logging.debug(f'm={m}') |
|
|
logging.debug(f'm={m}') |
|
|
self.assertIsNotNone(m) |
|
|
self.assertIsNotNone(m) |
|
|
|
|
|
|
|
|
|
|
|
# Prüft die Gültigkeit einer Postleitzahl ('^\d{5}$' genau 5 Ziffern) |
|
|
def test_plz(self): |
|
|
def test_plz(self): |
|
|
patterns = [pattern_plz,pattern_plz2] |
|
|
patterns = [pattern_plz,pattern_plz2] |
|
|
plz = ['42275','5600'] |
|
|
plz = ['42275','5600'] |
|
@ -35,30 +47,51 @@ class TestRegEgEx(unittest.TestCase): |
|
|
self.assertEqual([],m) |
|
|
self.assertEqual([],m) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Test für Suche |
|
|
def test_search(self): |
|
|
def test_search(self): |
|
|
m = pattern_char_only.search('Olli') |
|
|
m = pattern_char_only.search('Olli') |
|
|
logging.debug(f'm={m}') |
|
|
logging.debug(f'm={m}') |
|
|
|
|
|
#String passt auf die regular expression |
|
|
self.assertIsNotNone(m) |
|
|
self.assertIsNotNone(m) |
|
|
|
|
|
|
|
|
m = pattern_char_only.search('123456') |
|
|
m = pattern_char_only.search('123456') |
|
|
logging.debug(f'm={m}') |
|
|
logging.debug(f'm={m}') |
|
|
|
|
|
#Dieser String passt nicht. |
|
|
self.assertIsNone(m) |
|
|
self.assertIsNone(m) |
|
|
|
|
|
|
|
|
|
|
|
# Test zum Splitting (der Satz wird am ':' in die Worte zerlegt) |
|
|
def test_split(self): |
|
|
def test_split(self): |
|
|
m = pattern_quote.split(' Die:Würde:des:Menschen:ist:unantastbar.') |
|
|
m = pattern_quote.split(' Die:Würde:des:Menschen:ist:unantastbar.') |
|
|
logging.debug(f'm={m}') |
|
|
logging.debug(f'm={m}') |
|
|
self.assertIsNotNone(m) |
|
|
self.assertIsNotNone(m) |
|
|
|
|
|
|
|
|
|
|
|
#Die einzelnen Worte ausgeben |
|
|
|
|
|
for word in m: |
|
|
|
|
|
logging.debug(f' Wort: {word}') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Test für Ersetzung (substitution von Tab mit Space) |
|
|
def test_sub(self): |
|
|
def test_sub(self): |
|
|
m = pattern_tab.sub(' ','Satz\tmit\tTabulator.') |
|
|
m = pattern_tab.sub(' ','Satz\tmit\tTabulator.') |
|
|
logging.debug(f'm={m}') |
|
|
logging.debug(f'm={m}') |
|
|
|
|
|
|
|
|
|
|
|
# Test für einfache regular Expression ('O*') |
|
|
|
|
|
def test_simple_re(self): |
|
|
|
|
|
m = pattern_simple.search('Olli') |
|
|
|
|
|
logging.debug(f'm={m.span()}') |
|
|
|
|
|
self.assertTrue(m.end() > 0) |
|
|
|
|
|
m = pattern_simple.search('Luana') |
|
|
|
|
|
logging.debug(f'm={m.span()}') |
|
|
|
|
|
self.assertFalse(m.end() >0) |
|
|
|
|
|
|
|
|
|
|
|
# Test für den Iterator ('*Simpson') |
|
|
def test_finditer(self): |
|
|
def test_finditer(self): |
|
|
m = pattern_simpson.finditer('Homer Simpson, Marge Simpson, Bart Simpson, Lisa Simpson, Maggie Simpson') |
|
|
m = pattern_simpson.finditer('Homer Simpson, Marge Simpson, Bart Simpson, Lisa Simpson, Maggie Simpson') |
|
|
logging.debug(f'm={m}') |
|
|
logging.debug(f'm={m}') |
|
|
|
|
|
|
|
|
|
|
|
# Die einzelnen gefunden Teilstrings ausgeben. |
|
|
for name in m: |
|
|
for name in m: |
|
|
logging.debug(f'Name={name}') |
|
|
logging.debug(f'Name={name},Start bei: {name.start()}') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|