-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy path6-regular-expressions.py
69 lines (52 loc) · 1.78 KB
/
6-regular-expressions.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
"""
Getting Started with Python:
Regular Expressions
. = any character
^ match start of line
$ match end of line
* match 0 or more repetitions
+ match 1 or more repetitions
\d match a digit {m} m times -- \d{1,2}
\w match a word character (a-z, A-Z, 0-9, _)
(|) either or
[] set of characters
[^] characters not in set i.e. [^,]*
Help: http://regexone.com/
"""
# Example text parsing
import re
text1 = 'Monday, June 4, 1997, 2:38 p.m.'
text2 = 'Thursday, March 31, 2011, 12:38 a.m.'
# match the time
time1 = re.search('\d{1,2}:\d{2} (a.m.|p.m.)', text1)
time2 = re.search('\d{1,2}:\d{2} (a.m.|p.m.)', text2)
print('The time is ' + time1.group())
print('The time is ' + time2.group())
# match the day
day1 = re.search('[^,]*', text1)
day2 = re.search('[^,]*', text2)
print('The day is ' + day1.group())
print('The day is ' + day2.group())
# match the date
date1 = re.search('\w+ \d{1,2}, \d{4}', text1)
date2 = re.search('\w+ \d{1,2}, \d{4}', text2)
print('The date is ' + date1.group())
print('The date is ' + date2.group())
# match it all
match1 = re.search('([^,]*), (\w+ \d{1,2}, \d{4}), (\d{1,2}:\d{2}) (a.m.|p.m.)', text1)
match2 = re.search('([^,]*), (\w+ \d{1,2}, \d{4}), (\d{1,2}:\d{2}) (a.m.|p.m.)', text2)
print('Day: ' + match1.group(1) + '; Date: ' + match1.group(2) + \
'; Time: ' + match1.group(3) + ' ' + match1.group(4))
print('Day: ' + match2.group(1) + '; Date: ' + match2.group(2) + \
'; Time: ' + match2.group(3) + ' ' + match2.group(4))
# Another example
form1 = 'Name: Bob Michaels'
form2 = 'Name: Pam Wells'
form3 = 'Name: Bono'
namematch1 = re.search('Name: (.*)', form1)
namematch2 = re.search('Name: (.*)', form2)
namematch3 = re.search('Name: (.*)', form3)
print(namematch1.group(1))
print(namematch2.group(1))
print(namematch3.group(1))