Chapter 7 - Scripting with Python
What is the following output? >>> nongreedyHaRegex = re.compile(r'(Ha){3,5}?') >>> mo2 = nongreedyHaRegex.search('HaHaHaHaHa') >>> mo2.group()
'HaHaHa'
What is the output of the following?: >>> batRegex = re.compile(r'Bat(wo)*man') >>> mo1 = batRegex.search('The Adventures of Batman') >>> mo1.group()
'batman'
What is the output of the following?: >>> batRegex = re.compile(r'Bat(wo)*man') >>> mo1 = batRegex.search('The Adventures of Batman') >>> mo1.group() 'Batman' >>> mo2 = batRegex.search('The Adventures of Batwoman') >>> mo2.group()
'batwoman'
The ___ means "match zero or more"
*
The ___ means "match one or more".
+
The _____ character in a regular expression is called a wildcard and will match any character except for a newline.
. (or dot)
The ____ character flags the group that precedes it as an optional part of the pattern
?
the non-greedy version of the curly brackets has a ____ following the curly bracket
?
What is the fourth step to using regular expressions in python?
Call the Match object's group() method to return a string of the actual matched text.
What is the second step to using regular expressions in python?
Create a Regex object with the re.compile() function. (Remember to use a raw string.)
A negative character class will match all the characters that are in the character class. T/F
False
Passing a string value representing your regular expression to re.search() returns a Regex pattern object (or simply, a Regex object). T/F
False
The group preceding a plus must appear at least twice. It is not optional. T/F
False
What is the first step to using regular expressions in python?
Import the regex module with import re.
What is the third step to using regular expressions in python?
Pass the string you want to search into the Regex object's search() method. This returns a Match object.
Descriptions for a pattern of text
Regular expressions
_____ _____ allow you to specify a pattern of text to search for
Regular expressions
Does the following output come out to be True or False? >>> haRegex = re.compile(r'(Ha){3}') >>> mo2 = haRegex.search('Ha') >>> mo2 == None
True
If the pattern is found, the search() method returns a Match object. T/F
True
Passing 0 or nothing to the group() method will return the entire matched text. T/F
True
The following output for the code below is: 'CENSORED gave the secret documents to CENSORED.' >>> namesRegex = re.compile(r'Agent \w+') >>> namesRegex.sub('CENSORED', 'Agent Alice gave the secret documents to Agent Bob.') T/F
True
The following output for the code below is: ['cat', 'hat', 'sat', 'lat', 'mat'] >>> atRegex = re.compile(r'.at') >>> atRegex.findall('The cat in the hat sat on the flat mat.') T/F
True
the slightly shorter regex \d{3}-\d{3}-\d{4} matches the regex \d\d\d-\d\d\d-\d\d\d\d. T/F?
True
Any character that is not a numeric digit from 0 to 9.
\D
Any character that is not a space, tab, or newline.
\S
Any character that is not a letter, numeric digit, or the underscore character.
\W
Any numeric digit from 0 to 9.
\d
Any space, tab, or newline character. (Think of this as matching "space" characters.)
\s
Any letter, numeric digit, or the underscore character. (Think of this as matching "word" characters.)
\w
On the other hand, findall() will not return a Match object but_____
a list of strings
You can also use the caret symbol (^) at the start of a regex to indicate that a match must occur at the _________ of the searched text.
beginning
If you have a group that you want to repeat a specific number of times, follow the group in your regex with a number in ____
curly brackets
you can put a dollar sign ($) at the end of the regex to indicate the string must _____ with this regex pattern.
end
While search() will return a Match object of the first matched text in the searched string, the findall() method will return the strings of ____
every match
Python's regular expressions are ____ by default
greedy
What does passing re.VERBOSE as the second argument to re.compile() allow you to do?
ignore whitespace and comments inside the regular expression string
What must be done in order to use regex functions in python?
import re
What is the pipe (|) for?
match one of many expressions.
By placing a caret character (^) just after the character class's opening bracket, you can make a
negative character class
How do you create groups with regexes?
parentheses
Passing a string value representing your regular expression to _____ returns a Regex pattern object (or simply, a Regex object)
re.compile()
A Regex object's ______ method searches the string it is passed for any matches to the regex.
search()
You can define your own character class using
square brackets
(Ha){3,} will match ______?
three or more instances of (Ha)
If there are groups in the regular expression, then findall() will return a list of ______
tuples
(Ha){,5} will match _____?
zero to five instances of (Ha)