From bed21a150296a6b714bf5ad9271883525fd622cd Mon Sep 17 00:00:00 2001 From: Knut Finstermeier Date: Wed, 29 Apr 2026 07:10:29 +0200 Subject: [PATCH] fix: regex demo in lesson 7 --- lessons/lesson_07.ipynb | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lessons/lesson_07.ipynb b/lessons/lesson_07.ipynb index 9103379..c2c5863 100644 --- a/lessons/lesson_07.ipynb +++ b/lessons/lesson_07.ipynb @@ -272,16 +272,27 @@ "source": [ "import re\n", "\n", - "# # reading file\n", - "# with open(\"../data/towels.txt\", \"r\") as infile:\n", - "# story = infile.read()\n", + "# This text comes from the book \"20,000 Leagues Under the Sea\" by Jules Verne, published in 1870.\n", "\n", - "# # create pattern\n", - "# pattern = \".*[H|h]itch *[H|h]iker.*\"\n", - "# regex = re.compile(pattern)\n", + "story = \"\"\"On the 6th of November, 1867, the frigate Abraham Lincoln departed at 3:00 PM from Brooklyn pier.\n", + "The crew numbered 307 men and officers.\n", + "Captain Farragut had placed a reward of $2,000 for whoever first sighted the creature.\n", + "Professor Aronnax, a marine biologist from Paris, stood at the bow scanning the horizon.\n", + "The animal, if it exists, must be of considerable size — perhaps 200 feet in length.\n", + "The sea was calm; visibility extended roughly 15 nautical miles.\n", + "At latitude 31° 15' N, longitude 136° 42' E, they found nothing.\n", + "After 3 weeks with no sightings, the crew grew restless.\n", + "Then, on November 28th at 11:17 PM, the lookout cried: Object sighted — bearing 315 degrees!\n", + "The creature emitted a pale phosphorescent light and moved at approximately 40 knots.\n", + "Aronnax estimated its mass at no less than 1,500 tons.\n", + "Impossible, said Conseil quietly, and yet — there it is.\"\"\"\n", "\n", - "# # search for pattern and print each line with the pattern in it\n", - "# print(regex.findall(story))" + "# create pattern\n", + "pattern = \".*\\d{1,2}:\\d{2} PM.*\"\n", + "regex = re.compile(pattern)\n", + "\n", + "# search for pattern and print each line with the pattern in it\n", + "print(regex.findall(story))" ] }, { @@ -292,7 +303,7 @@ "- certain strings have specific meanings:\n", " - `.*` = any number of any character before/after our pattern except `\\n`, including 0 observations\n", " - `*` = any number of any character within our pattern, including 0 observations\n", - " - `[H|h]` = one character, eiter an `H` or an `h`\n", + " - `\\d{1,2}` = one or two digits\n", "- when compiling the search pattern, you can include certain flags\n", " - `re.IGNORECASE` to have case insensitive matching\n", " - `re.DOTALL` to have the `.` match all characters incl. the line end character `\\n`\n", @@ -1359,7 +1370,7 @@ "metadata": { "celltoolbar": "Slideshow", "kernelspec": { - "display_name": "default", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -1373,7 +1384,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.14.4" + "version": "3.11.0" } }, "nbformat": 4,