forked from albertsun/Intro-Data-Journalism-With-Python
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsample6.py
35 lines (27 loc) · 1.3 KB
/
sample6.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# here we're going to use regular expressions to extract bits of text
# regular expressions are a sort of "sub" language that's used within other programming languages to match patterns
# the syntax looks a little obtuse at first, but they are an extremely powerful and useful tool
# here's a reference about them http://www.regular-expressions.info/
# to use regular expressions in Python, we have to import the regular expressions module, named re
# the import statement pulls in a bundle of functionality that we can access through the variable re
import re
# here's the content we'll look for things in
# go through and run this file three times.
content = """
AAMW-521 HSE/VILLA/PAL HELLEN ROM 1 CU
401 SEM W 3:30-6:30PM KUTTNER A
CROSS LISTED: ARTH-521 CLST-521
MAX W/CROSS LIST: 13
"""
# regular expression 1
findtime = re.compile(r'(\d{1,2}:\d\d)')
matches = findtime.findall(content)
print matches
# regular expression 2, uncomment the lines below to run it
# findinstructor = re.compile(r'(\w+ [A-Z]\n)')
# matches = findinstructor.search(content)
# print matches.groups()
# regular expression 3, uncomment the lines below to run it
# findtimeranges = re.compile(r'(\d{1,2}(:\d\d)?)-((\d{1,2}(:\d\d)?)(([AP]M)|NOON))')
# matches = findtimeranges.search(content)
# print matches.groups()