Skip to content

Commit 899c7f8

Browse files
author
Charlotte Weaver
committed
Cleaned up lesson 10, clarified solution with comments
1 parent bf867e4 commit 899c7f8

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11

22
import re
33

4+
# Initialize an empyt paragraph
45
paragraph = []
56
found_match = False
6-
# Gold E. Locks (case insensitive, E. or E) Three bears or 3 bears 571 209-4000
7+
# These are the regexs that we are going to use
8+
# Gold E. Locks (case insensitive, E. or E) Three bears or 3 bears 571 209-4000
79
patterns = ['gold e\.? locks', 'three bears', '3 bears', '\(?571\)? ?209-?4000']
810
with open('evidence.txt', 'r') as fh:
9-
for line in fh:
10-
if line == '\n':
11-
if found_match:
12-
print ''.join(paragraph)
13-
found_match = False
14-
paragraph = []
15-
16-
for pattern in patterns:
17-
if re.search(pattern, line, re.IGNORECASE):
18-
found_match = True
19-
paragraph.append(line)
11+
for line in fh:
12+
# If it is the end of a paragraph,
13+
# print the paragraph if we found a match to the regex
14+
if line == '\n':
15+
if found_match:
16+
print ''.join(paragraph)
17+
# Reset found_match and paragraph for the next paragraph
18+
found_match = False
19+
paragraph = []
20+
21+
# seach each pattern, and if there is a match, set found_match flag to True
22+
for pattern in patterns:
23+
if re.search(pattern, line, re.IGNORECASE):
24+
found_match = True
25+
# Append line to paragraph
26+
paragraph.append(line)
2027

Lesson10_Regexs/RegularExpressions.ipynb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
"\n",
99
"Up until now, to search in text we have used string methods find, startswith, endswith, etc. But sometimes you need more power.\n",
1010
"\n",
11-
"Regular expressions (regex) are their own little language that allows you to search through text and find matches with incredibly complex patterns. \n",
11+
"Regular expressions are their own little language that allows you to search through text and find matches with incredibly complex patterns. \n",
1212
"\n",
1313
"A regular expression, also referred to as \"regex\" or \"regexp\", provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters.\n",
1414
"\n",
15-
"To use regular you must import python's regex library `re`\n",
15+
"To use regular you need to import python's regex library `re`\n",
1616
"https://docs.python.org/2/library/re.html"
1717
]
1818
},
@@ -35,7 +35,7 @@
3535
},
3636
"outputs": [],
3737
"source": [
38-
"# To run the examplse we are going to use some of the logs from the \n",
38+
"# To run the examples we are going to use some of the logs from the \n",
3939
"# django project, a web framework for python\n",
4040
"\n",
4141
"django_logs = '''commit 722344ee59fb89ea2cd5b906d61b35f76579de4e\n",
@@ -103,7 +103,7 @@
103103
"source": [
104104
"## Searching\n",
105105
"\n",
106-
"The most simple thing you can do with regexs in python is search through text to see if there is a match. To do this you use the methods `search` or `match`. `match` only checks if it matches at the beginning of the string and `search` check the whole string.\n",
106+
"The simplest thing you can do with regexs in python is search through text to see if there is a match. To do this you use the methods `search` or `match`. `match` only checks if it matches at the beginning of the string and `search` check the whole string.\n",
107107
"\n",
108108
" re.match(pattern, string) \n",
109109
" re.search(pattern, string) "
@@ -162,7 +162,7 @@
162162
"metadata": {},
163163
"source": [
164164
"### TRY IT\n",
165-
"search for the word May in the django logs"
165+
"Search for the word May in the django logs"
166166
]
167167
},
168168
{
@@ -389,6 +389,7 @@
389389
"times = re.findall(time_pattern, django_logs)\n",
390390
"print times\n",
391391
"\n",
392+
"# Unpacking the tuple in the first line\n",
392393
"for hours, mins in times:\n",
393394
" print \"{} hr {} min\".format(hours, mins)"
394395
]
@@ -444,7 +445,7 @@
444445
"\n",
445446
"\n",
446447
"0. Import re\n",
447-
"1. Initailize a variable called paragraph to be an empty list and a variable called found_match to false.\n",
448+
"1. Initialize a variable called paragraph to be an empty list and a variable called found_match to false.\n",
448449
"2. Create a list of patterns to match and store in variable called patterns\n",
449450
"3. Read in test file 'evidence.txt'.\n",
450451
"4. For line in evidence:\n",

0 commit comments

Comments
 (0)