You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
2.9 KiB
102 lines
2.9 KiB
10 months ago
|
import re
|
||
|
import unittest
|
||
|
import logging
|
||
|
|
||
|
pattern_char_only = re.compile('^[A-Z][a-z]+$')
|
||
9 months ago
|
pattern_digits_only = re.compile(r'\d+')
|
||
|
pattern_no_digits = re.compile(r'\D+')
|
||
10 months ago
|
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')
|
||
9 months ago
|
pattern_simple = re.compile('O*')
|
||
10 months ago
|
|
||
|
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG)
|
||
9 months ago
|
|
||
|
# Unittest für die Demonstration von regular expressions (re)
|
||
10 months ago
|
class TestRegEgEx(unittest.TestCase):
|
||
9 months ago
|
|
||
|
# match: Passt der String auf die re
|
||
10 months ago
|
def test_match(self):
|
||
|
m = pattern_char_only.match('Olli')
|
||
9 months ago
|
|
||
10 months ago
|
logging.debug(f'm={m}')
|
||
9 months ago
|
self.assertTrue(m.span()[1] >0)
|
||
10 months ago
|
|
||
9 months ago
|
# Sucht alle Vorkommnisse der re im String
|
||
10 months ago
|
def test_findall(self):
|
||
9 months ago
|
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)}')
|
||
10 months ago
|
m = pattern_char_only.findall('Olli')
|
||
|
logging.debug(f'm={m}')
|
||
|
self.assertIsNotNone(m)
|
||
|
|
||
9 months ago
|
# Prüft die Gültigkeit einer Postleitzahl ('^\d{5}$' genau 5 Ziffern)
|
||
10 months ago
|
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)
|
||
|
|
||
|
|
||
9 months ago
|
# Test für Suche
|
||
10 months ago
|
def test_search(self):
|
||
|
m = pattern_char_only.search('Olli')
|
||
|
logging.debug(f'm={m}')
|
||
9 months ago
|
#String passt auf die regular expression
|
||
10 months ago
|
self.assertIsNotNone(m)
|
||
|
|
||
|
m = pattern_char_only.search('123456')
|
||
|
logging.debug(f'm={m}')
|
||
9 months ago
|
#Dieser String passt nicht.
|
||
10 months ago
|
self.assertIsNone(m)
|
||
|
|
||
9 months ago
|
# Test zum Splitting (der Satz wird am ':' in die Worte zerlegt)
|
||
10 months ago
|
def test_split(self):
|
||
9 months ago
|
m = pattern_quote.split(' Die:Würde:des:Menschen:ist:unantastbar.')
|
||
10 months ago
|
logging.debug(f'm={m}')
|
||
|
self.assertIsNotNone(m)
|
||
|
|
||
9 months ago
|
#Die einzelnen Worte ausgeben
|
||
|
for word in m:
|
||
|
logging.debug(f' Wort: {word}')
|
||
|
|
||
|
|
||
|
# Test für Ersetzung (substitution von Tab mit Space)
|
||
10 months ago
|
def test_sub(self):
|
||
|
m = pattern_tab.sub(' ','Satz\tmit\tTabulator.')
|
||
|
logging.debug(f'm={m}')
|
||
|
|
||
9 months ago
|
# 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')
|
||
10 months ago
|
def test_finditer(self):
|
||
|
m = pattern_simpson.finditer('Homer Simpson, Marge Simpson, Bart Simpson, Lisa Simpson, Maggie Simpson')
|
||
|
logging.debug(f'm={m}')
|
||
|
|
||
9 months ago
|
# Die einzelnen gefunden Teilstrings ausgeben.
|
||
10 months ago
|
for name in m:
|
||
9 months ago
|
logging.debug(f'Name={name},Start bei: {name.start()}')
|
||
10 months ago
|
|
||
|
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
unittest.main()
|
||
|
|