Skip to content

Commit 6053eda

Browse files
committed
Updated the regular expressions lesson
1 parent fa9c611 commit 6053eda

File tree

2 files changed

+53
-160
lines changed

2 files changed

+53
-160
lines changed

17_regular_expressions.ipynb

Lines changed: 26 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@
66
"source": [
77
"# Regular Expressions\n",
88
"\n",
9-
"In the last session we tried to interpret strings as valid heights and weights. This involved looking for text such as \"meter\" or \"kilogram\" in the string, and then extracting the number. This process is called pattern matching, and is best undertaken using a regular expression.\n",
9+
"In the error handling session we tried to interpret strings as valid heights and weights. This involved looking for text such as \"meter\" or \"kilogram\" in the string, and then extracting the number. This process is called pattern matching, and is best undertaken using a regular expression.\n",
1010
"\n",
1111
"Regular expressions have a long history and are available in most programming languages. Python implements a standards-compliant regular expression module, which is called `re`."
1212
]
1313
},
1414
{
1515
"cell_type": "code",
1616
"execution_count": null,
17-
"metadata": {
18-
"collapsed": true
19-
},
17+
"metadata": {},
2018
"outputs": [],
2119
"source": [
2220
"import re"
@@ -32,9 +30,7 @@
3230
{
3331
"cell_type": "code",
3432
"execution_count": null,
35-
"metadata": {
36-
"collapsed": true
37-
},
33+
"metadata": {},
3834
"outputs": [],
3935
"source": [
4036
"h = \"2 meters\""
@@ -69,9 +65,7 @@
6965
{
7066
"cell_type": "code",
7167
"execution_count": null,
72-
"metadata": {
73-
"collapsed": true
74-
},
68+
"metadata": {},
7569
"outputs": [],
7670
"source": [
7771
"m = re.search(\"meters\", h)"
@@ -96,9 +90,7 @@
9690
{
9791
"cell_type": "code",
9892
"execution_count": null,
99-
"metadata": {
100-
"collapsed": true
101-
},
93+
"metadata": {},
10294
"outputs": [],
10395
"source": [
10496
"h = \"2 meter\""
@@ -107,9 +99,7 @@
10799
{
108100
"cell_type": "code",
109101
"execution_count": null,
110-
"metadata": {
111-
"collapsed": true
112-
},
102+
"metadata": {},
113103
"outputs": [],
114104
"source": [
115105
"m = re.search(\"meters?\", h)"
@@ -134,9 +124,7 @@
134124
{
135125
"cell_type": "code",
136126
"execution_count": null,
137-
"metadata": {
138-
"collapsed": true
139-
},
127+
"metadata": {},
140128
"outputs": [],
141129
"source": [
142130
"m = re.search(\"meters?$\", h)"
@@ -161,9 +149,7 @@
161149
{
162150
"cell_type": "code",
163151
"execution_count": null,
164-
"metadata": {
165-
"collapsed": true
166-
},
152+
"metadata": {},
167153
"outputs": [],
168154
"source": [
169155
"h = \"2 m\""
@@ -172,9 +158,7 @@
172158
{
173159
"cell_type": "code",
174160
"execution_count": null,
175-
"metadata": {
176-
"collapsed": true
177-
},
161+
"metadata": {},
178162
"outputs": [],
179163
"source": [
180164
"m = re.search(\"(m|meters?)$\", h)"
@@ -199,9 +183,7 @@
199183
{
200184
"cell_type": "code",
201185
"execution_count": null,
202-
"metadata": {
203-
"collapsed": true
204-
},
186+
"metadata": {},
205187
"outputs": [],
206188
"source": [
207189
"h = \"2 meters\""
@@ -210,9 +192,7 @@
210192
{
211193
"cell_type": "code",
212194
"execution_count": null,
213-
"metadata": {
214-
"collapsed": true
215-
},
195+
"metadata": {},
216196
"outputs": [],
217197
"source": [
218198
"m = re.search(\"\\d (m|meters?)$\", h)"
@@ -237,9 +217,7 @@
237217
{
238218
"cell_type": "code",
239219
"execution_count": null,
240-
"metadata": {
241-
"collapsed": true
242-
},
220+
"metadata": {},
243221
"outputs": [],
244222
"source": [
245223
"h = \"10 meters\""
@@ -248,9 +226,7 @@
248226
{
249227
"cell_type": "code",
250228
"execution_count": null,
251-
"metadata": {
252-
"collapsed": true
253-
},
229+
"metadata": {},
254230
"outputs": [],
255231
"source": [
256232
"m = re.search(\"\\d+ (m|meters?)$\", h)"
@@ -275,9 +251,7 @@
275251
{
276252
"cell_type": "code",
277253
"execution_count": null,
278-
"metadata": {
279-
"collapsed": true
280-
},
254+
"metadata": {},
281255
"outputs": [],
282256
"source": [
283257
"h = \"1.5 meters\""
@@ -286,9 +260,7 @@
286260
{
287261
"cell_type": "code",
288262
"execution_count": null,
289-
"metadata": {
290-
"collapsed": true
291-
},
263+
"metadata": {},
292264
"outputs": [],
293265
"source": [
294266
"m = re.search(\"\\d+\\.?\\d* (m|meters?)$\", h)"
@@ -313,9 +285,7 @@
313285
{
314286
"cell_type": "code",
315287
"execution_count": null,
316-
"metadata": {
317-
"collapsed": true
318-
},
288+
"metadata": {},
319289
"outputs": [],
320290
"source": [
321291
"h = \"some 1.8 meters\""
@@ -324,9 +294,7 @@
324294
{
325295
"cell_type": "code",
326296
"execution_count": null,
327-
"metadata": {
328-
"collapsed": true
329-
},
297+
"metadata": {},
330298
"outputs": [],
331299
"source": [
332300
"m = re.search(\"^\\d+\\.?\\d* (m|meters?)$\", h)"
@@ -335,9 +303,7 @@
335303
{
336304
"cell_type": "code",
337305
"execution_count": null,
338-
"metadata": {
339-
"collapsed": true
340-
},
306+
"metadata": {},
341307
"outputs": [],
342308
"source": [
343309
"m"
@@ -353,9 +319,7 @@
353319
{
354320
"cell_type": "code",
355321
"execution_count": null,
356-
"metadata": {
357-
"collapsed": true
358-
},
322+
"metadata": {},
359323
"outputs": [],
360324
"source": [
361325
"h = \" 1.8 METers \""
@@ -364,9 +328,7 @@
364328
{
365329
"cell_type": "code",
366330
"execution_count": null,
367-
"metadata": {
368-
"collapsed": true
369-
},
331+
"metadata": {},
370332
"outputs": [],
371333
"source": [
372334
"m = re.search(\"^\\s*\\d+\\.?\\d*\\s*(m|meters?)\\s*$\", h, re.IGNORECASE)"
@@ -407,9 +369,7 @@
407369
{
408370
"cell_type": "code",
409371
"execution_count": null,
410-
"metadata": {
411-
"collapsed": true
412-
},
372+
"metadata": {},
413373
"outputs": [],
414374
"source": [
415375
"m = re.search(\"^\\s*(\\d+\\.?\\d*)\\s*(m|meters?)\\s*$\", h, re.IGNORECASE)"
@@ -434,9 +394,7 @@
434394
{
435395
"cell_type": "code",
436396
"execution_count": null,
437-
"metadata": {
438-
"collapsed": true
439-
},
397+
"metadata": {},
440398
"outputs": [],
441399
"source": [
442400
"def string_to_height(height):\n",
@@ -452,9 +410,7 @@
452410
{
453411
"cell_type": "code",
454412
"execution_count": null,
455-
"metadata": {
456-
"collapsed": true
457-
},
413+
"metadata": {},
458414
"outputs": [],
459415
"source": [
460416
"h = string_to_height(\" 1.5 meters \")"
@@ -499,9 +455,7 @@
499455
{
500456
"cell_type": "code",
501457
"execution_count": null,
502-
"metadata": {
503-
"collapsed": true
504-
},
458+
"metadata": {},
505459
"outputs": [],
506460
"source": [
507461
"def get_number_and_unit(s):\n",
@@ -521,9 +475,7 @@
521475
{
522476
"cell_type": "code",
523477
"execution_count": null,
524-
"metadata": {
525-
"collapsed": true
526-
},
478+
"metadata": {},
527479
"outputs": [],
528480
"source": []
529481
}
@@ -544,7 +496,7 @@
544496
"name": "python",
545497
"nbconvert_exporter": "python",
546498
"pygments_lexer": "ipython3",
547-
"version": "3.5.3"
499+
"version": "3.5.2"
548500
}
549501
},
550502
"nbformat": 4,

0 commit comments

Comments
 (0)