-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathch_7_practice_questions.txt
149 lines (81 loc) · 4.91 KB
/
ch_7_practice_questions.txt
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
## 1. What is the function that creates Regex objects?
re.compile()
## 2. Why are raw strings often used when creating Regex objects?
So you don't have to escape backslashes. For example, instead of needing to use '\\d' for a digit, you can instead use r'\d'.
## 3. What does the search() method return?
Either a Match object containing the first occurrence of a regular expression, or None.
## 4. How do you get the actual strings that match the pattern from a Match object?
By calling the group method on the Match object.
## 5. In the regex created from r'(\d\d\d)-(\d\d\d-\d\d\d\d)', what does group 0 cover? Group 1? Group 2?
group 0 covers the whole matching text, for example '555-555-5555'
group 1 covers the group in the first set of parentheses, for example '555'
group 2 covers the group in the second set of parentheses, for example '555-5555'
## 6. Parentheses and periods have specific meanings in regular expression syntax. How would you specify that you want a regex to match actual parentheses and period characters?
You could escape these characters:
\(
\)
\.
## 7. The findall() method returns a list of strings or a list of tuples of strings. What makes it return one or the other?
If you use grouping parentheses, (), in the regular expression it will return a list of tuples. If you don't use groups, it will return a list of strings.
## 8. What does the | character signify in regular expressions?
It signifies Boolean or.
## 9. What two things does the ? character signify in regular expressions?
It can signify that part of a regular expression is optional (i.e., it occurs 0 or 1 times), or that nongreedy matching should be used.
## 10. What is the difference between the + and * characters in regular expressions?
+ means match one or more times.
* means match 0 or more times.
## 11. What is the difference between {3} and {3,5} in regular expressions?
{3} means match exactly 3 times.
{3,5} means match 3, 4, or 5 times.
## 12. What do the \d, \w, and \s shorthand character classes signify in regular expressions?
\d stands for a numeric digit.
\w stands for any letter, numeric digit, or the underscore character.
\s stands for any space, tab, or newline character.
## 13. What do the \D, \W, and \S shorthand character classes signify in regular expressions?
\D stands for any character that is not a numeric digit.
\W stands for any character that is not a letter, digit, or underscore.
\S stands for any character that is not a space, tab, or newline.
## 14. How do you make a regular expression case-insensitive?
use the re.IGNORECASE (or re.I) argument in re.compile().
## 15. What does the . character normally match? What does it match if re.DOTALL is passed as the second argument to re.compile()?
. normally matches any one character except the newline.
If the re.DOTALL argument is used, a . matches any one character including the newline.
## 16. What is the difference between these two: .* and .*?
.* uses greedy matching.
.*? is the nongreedy version.
## 17. What is the character class syntax to match all numbers and lowercase letters?
[a-z0-9]
## 18. If numRegex = re.compile(r'\d+'), what will numRegex.sub('X', '12 drummers, 11 pipers, five rings, 3 hens') return?
'X drummers, X pipers, five rings, X hens'
## 19. What does passing re.VERBOSE as the second argument to re.compile() allow you to do?
It allows you to ignore white space and comments in the regular expression string.
## 20. How would you write a regex that matches a number with commas for every three digits? It must match the following:
'42'
'1,234'
'6,368,745'
but not the following:
'12,34,567' (which has only two digits between the commas)
'1234' (which lacks commas)
r'((^\d{1,3},)(\d{3},)*\d{3}$)|(^\d{,3}$)'
Much, much better way: r'^\d{1,3}(,\d{3})*$'
## 21. How would you write a regex that matches the full name of someone whose last name is Nakamoto? You can assume that the first name that comes before it will always be one word that begins with a capital letter. The regex must match the following:
'Satoshi Nakamoto'
'Alice Nakamoto'
'Robocop Nakamoto'
but not the following:
'satoshi Nakamoto' (where the first name is not capitalized)
'Mr. Nakamoto' (where the preceding word has a nonletter character)
'Nakamoto' (which has no first name)
'Satoshi nakamoto' (where Nakamoto is not capitalized)
r'[A-Z][a-z]* Nakamoto'
## 22. How would you write a regex that matches a sentence where the first word is either Alice, Bob, or Carol; the second word is either eats, pets, or throws; the third word is apples, cats, or baseballs; and the sentence ends with a period? This regex should be case-insensitive. It must match the following:
'Alice eats apples.'
'Bob pets cats.'
'Carol throws baseballs.'
'Alice throws Apples.'
'BOB EATS CATS.'
but not the following:
'Robocop eats apples.'
'ALICE THROWS FOOTBALLS.'
'Carol eats 7 cats.'
re.compile(r'(Alice|Bob|Carol) (eats|pets|throws) (apples|cats|baseballs)\.', re.IGNORECASE)