Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Python-Programmierkurs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Institut für Bibliotheks- und Informationswissenschaft
Python-Programmierkurs
Commits
23483f3f
Commit
23483f3f
authored
6 years ago
by
Prof. Dr. Robert Jäschke
Browse files
Options
Downloads
Patches
Plain Diff
+Tipps
parent
1e5c3d3f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
notebooks/uebung_regex.ipynb
+87
-3
87 additions, 3 deletions
notebooks/uebung_regex.ipynb
with
87 additions
and
3 deletions
notebooks/uebung_regex.ipynb
+
87
−
3
View file @
23483f3f
...
...
@@ -21,7 +21,7 @@
"Ändern Sie den folgenden regulären Ausdruck, so dass alle \n",
"positiven Muster erkannt werden, aber kein negatives Muster.\n",
"\"\"\"\n",
"muster = re.compile(\"
\") \n",
"muster = re.compile(\"
Z
\") \n",
"positive = [\n",
" \"rap them\",\n",
" \"tapeth\",\n",
...
...
@@ -47,13 +47,97 @@
"# 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 positive Muster wurden nicht erkannt:\", \", \".join(positive_not_matched))\n",
" print(\"Folgende positive
n
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",
" print(\"Folgende negativen Muster wurden erkannt:\", \", \".join(negative_matched))\n"
" 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.\")"
]
},
{
"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": []
}
],
"metadata": {
...
...
%% Cell type:markdown id: tags:
# 12. Übungsblatt
Mit diesem Python-Code können Sie Ihren regulären Ausdruck testen:
%% Cell type:code id: tags:
```
import re
"""
Ändern Sie den folgenden regulären Ausdruck, so dass alle
positiven Muster erkannt werden, aber kein negatives Muster.
"""
muster = re.compile("
")
muster = re.compile("
Z
")
positive = [
"rap them",
"tapeth",
"apth",
"wrap/try",
"sap tray",
"87ap9th",
"apothecary"
]
negative = [
"aleht",
"happy them",
"tarpth",
"Apt",
"peth",
"tarreth",
"ddapdg",
"apples",
"shape the"
]
# testen, ob alle positiven Muster richtig erkannt werden
positive_not_matched = [s for s in positive if not muster.findall(s)]
if positive_not_matched:
print("Folgende positive Muster wurden nicht erkannt:", ", ".join(positive_not_matched))
print("Folgende positive
n
Muster wurden nicht erkannt:", ", ".join(positive_not_matched))
# testen, ob keine negativen Muster erkannt werden
negative_matched = [s for s in negative if muster.findall(s)]
if negative_matched:
print("Folgende negativen Muster wurden erkannt:", ", ".join(negative_matched))
if not positive_not_matched and not negative_matched:
print("Herzlichen Glückwunsch, Sie haben das richtige Muster gefunden!")
```
%% Cell type:markdown id: tags:
Nachfolgend noch einige Beispiele zur Verwendung von regulären Ausdrücke mit Python:
%% Cell type:code id: tags:
```
# Funktionen für reguläre Ausdrücke werden im Modul "re" bereitgestellt
import re
# die Methode "findall" findet alle Teilzeichenketten in einer Zeichenkette,
# die auf das angegebene Muster passen
re.findall("cat|dog", "This sentence contains a dog.")
```
%% Cell type:code id: tags:
```
# die Methode "match" testet, ob die gesamte Zeichenkette auf das angegebene Muster passt
re.match(".*dog\\.", "This sentence contains a dog.")
```
%% Cell type:code id: tags:
```
re.match(".*(cat|dog)", "This sentence contains a cat.")
```
%% Cell type:markdown id: tags:
Mit "match" können wir beispielsweise unsere Ergebnisse für Aufgabe 1 prüfen:
%% Cell type:code id: tags:
```
if re.match("[hc]?at", "cat"):
print("Wort wurde erkannt")
else:
print("Wort wurde nicht erkannt")
```
%% Cell type:markdown id: tags:
%% Cell type:code id: tags:
```
# Es gibt eine umfangreiche eingebaute Hilfe:
help(re)
```
%% Cell type:code id: tags:
```
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment