Pattern Matching with Regular Expressions
185
13. What do the
\D
,
\W
, and
\S
shorthand character classes signify in
regular expressions?
14. What is the difference between
.*
and
.*?
?
15. What is the character class syntax
to match all numbers and
lowercase letters?
16. How do you make a regular expression case-insensitive?
17. What does the
.
character normally match? What does it match if
re.DOTALL
is passed as the second argument to
re.compile()
?
18. If
numRegex = re.compile(r'\d+')
, what will
numRegex.sub('X', '12 drummers,
11 pipers,
five rings, 3 hens')
return?
19. What does passing
re.VERBOSE
as the second argument to
re.compile()
allow you to do?
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)
21. How would you write a regex that matches
the full name of someone
whose last name is Watanabe? 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:
•
'Haruto Watanabe'
•
'Alice Watanabe'
•
'RoboCop Watanabe'
but not the following:
•
'haruto Watanabe'
(where the first name is not capitalized)
•
'Mr. Watanabe'
(where the preceding word has a nonletter character)
•
'Watanabe'
(which has no first name)
•
'Haruto watanabe'
(where Watanabe is not capitalized)
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.'
186
Chapter 7
but not the following:
•
'RoboCop eats apples.'
•
'ALICE THROWS FOOTBALLS.'
•
'Carol eats 7 cats.'
Dostları ilə paylaş: