Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions lessons/lesson_07.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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))"
]
},
{
Expand All @@ -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",
Expand Down Expand Up @@ -1359,7 +1370,7 @@
"metadata": {
"celltoolbar": "Slideshow",
"kernelspec": {
"display_name": "default",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -1373,7 +1384,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.14.4"
"version": "3.11.0"
}
},
"nbformat": 4,
Expand Down