Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 12. Übungsblatt\n",
"\n",
"Mit diesem Python-Code können Sie Ihren regulären Ausdruck testen:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"\n",
"\"\"\"\n",
"Ändern Sie den folgenden regulären Ausdruck, so dass alle \n",
"positiven Muster erkannt werden, aber kein negatives Muster.\n",
"\"\"\"\n",
"positive = [\n",
" \"rap them\",\n",
" \"tapeth\",\n",
" \"apth\",\n",
" \"wrap/try\",\n",
" \"sap tray\",\n",
" \"87ap9th\",\n",
" \"apothecary\"\n",
"]\n",
"\n",
"negative = [\n",
" \"aleht\",\n",
" \"happy them\",\n",
" \"tarpth\",\n",
" \"Apt\",\n",
" \"peth\",\n",
" \"tarreth\",\n",
" \"ddapdg\",\n",
" \"apples\",\n",
" \"shape the\"\n",
"]\n",
"\n",
"# testen, ob alle positiven Muster richtig erkannt werden\n",
"positive_not_matched = [s for s in positive if not muster.findall(s)]\n",
"if positive_not_matched:\n",
" print(\"Folgende positiven Muster wurden nicht erkannt:\", \", \".join(positive_not_matched))\n",
"\n",
"# testen, ob keine negativen Muster erkannt werden\n",
"negative_matched = [s for s in negative if muster.findall(s)]\n",
"if negative_matched:\n",
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
" print(\"Folgende negativen Muster wurden erkannt:\", \", \".join(negative_matched))\n",
"\n",
"if not positive_not_matched and not negative_matched:\n",
" print(\"Herzlichen Glückwunsch, Sie haben das richtige Muster gefunden!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nachfolgend noch einige Beispiele zur Verwendung von regulären Ausdrücke mit Python:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Funktionen für reguläre Ausdrücke werden im Modul \"re\" bereitgestellt\n",
"import re\n",
"\n",
"# die Methode \"findall\" findet alle Teilzeichenketten in einer Zeichenkette,\n",
"# die auf das angegebene Muster passen \n",
"re.findall(\"cat|dog\", \"This sentence contains a dog.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# die Methode \"match\" testet, ob die gesamte Zeichenkette auf das angegebene Muster passt \n",
"re.match(\".*dog\\\\.\", \"This sentence contains a dog.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"re.match(\".*(cat|dog)\", \"This sentence contains a cat.\")"
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mit \"match\" können wir beispielsweise unsere Ergebnisse für Aufgabe 1 prüfen:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if re.match(\"[hc]?at\", \"cat\"):\n",
" print(\"Wort wurde erkannt\")\n",
"else:\n",
" print(\"Wort wurde nicht erkannt\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Es gibt eine umfangreiche eingebaute Hilfe:\n",
"help(re)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []