-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_regexps.py
More file actions
67 lines (40 loc) · 1.7 KB
/
test_regexps.py
File metadata and controls
67 lines (40 loc) · 1.7 KB
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
import re
import unittest
class TestRegExpBasics(unittest.TestCase):
def test_raw_strings(self):
self.assertEqual('hello', r'hello')
raw_string = r'\n'
self.assertEqual(len(raw_string), 2)
self.assertEqual(raw_string[0], '\\')
self.assertEqual(raw_string[1], 'n')
def test_patterns(self):
s = "dead parrot"
pattern = re.compile(r'(dead)')
self.assertIsNotNone(pattern.match(s))
self.assertIsNotNone(re.match(r'(dead)', s))
self.assertEqual(len(pattern.match(s).groups()), 1)
self.assertEqual(pattern.match(s).groups(), re.match(r'(dead)', s).groups())
def test_substitution(self):
start_string = 'dead parrot'
end_string = 'live parrot'
self.assertEqual(start_string.replace('dead', 'live'), end_string)
self.assertEqual(re.sub(r'dead', 'live', start_string), end_string)
self.assertEqual(start_string.replace('dead', 'live'), re.sub('dead', 'live', start_string))
def test_advanced_substitution(self):
s = 'Polly! Polly Parrot!'
self.assertEqual(s.replace('Polly', 'Hey'), 'Hey! Hey Parrot!')
self.assertEqual(re.sub(r'^Polly', 'Hey', s), 'Hey! Polly Parrot!')
def test_findall(self):
names = "Emma Jones Eric Idle Steven Jones Michael Palin"
joneses = re.findall(r'\w+ Jones', names)
self.assertEqual(len(joneses), 2)
self.assertEqual(joneses, ['Emma Jones', 'Steven Jones'])
def test_named_groups(self):
date_string = "2015-03-07"
pattern = r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})'
match = re.match(pattern, date_string)
groups = match.groupdict()
for key in ['year', 'month', 'day']:
self.assertTrue(key in groups.keys())
for key, value in [('year', '2015'), ('month', '03'), ('day', '07')]:
self.assertEqual(groups[key], value)