Chapter 7: Pattern Matching with Regular Expressions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Consider the following code: phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') mo = phoneNumRegex.search('123-456-7890') What will print(mo.groups()) return?

Returns the tuple ('123', '456-7890')

By placing a __________ just after the character class's opening bracket you can make a __________ character class that will match all characters not in the character class.

caret (^) negative (p. 174)

You can place a __________ at the START of the regex to indicate that a match must BEGIN with the pattern.

caret (^) (p. 174)

You can place a __________ at the END of the regex to indicate that a regex must END with the pattern..

dollar sign($) (p. 174)

In regular expressions, what is the difference between the group () method and the findall() method?

group() method will return the first match where the findall() method will return all matches. findall() also does not call the .search() method on the regex because it does not return a match object.

The (r'\d$') regular expression string matches strings that END with a __________.

numeric digit 0-9 (p. 174)

The pipe character is essentially saying __________.

or re.compile(r'Batman|Robin') could be read as "match Batman or Robin)

Adding ___________ will create groups in the regex that you can search using the .group() regex method.

parenthesis (p. 166)

The | character is called the __________.

pipe

You can use re.DOTALL, re.IGNORECASE, and re.VERBOSE all as the second argument by separating them with the _____________ character.

pipe |

The ___________ means to match a group in parenthesis occurring before it at least one or more times in the text.

plus (+) (p. 170)

The __________ can have two meanings in regular expressions (regex), declaring a non greedy match or flagging an optional group.

question mark (?) (p. 171)

The non greedy version of the braces that returns the shortest possible match has the closing braces followed by a __________.

question mark (?) (p. 171)

By passing __________ as the second argument to re.compile() you can make the .* character match all characters including newline characters (\n).

re.DOTALL

By passing __________ as the second argument to re.compile() you can make the regex case insensitive.

re.I or re.IGNORECASE

By passing __________ as the second argument to re.compile() you can make the regex ignore white space.

re.VERBOSE

The ___________ means to match a group in parenthesis occurring before it zero or more times in the text.

star or asterisk (*) (p.169)

The . (dot) character in a regular expression is called a __________ and will match any character except for a newline.

wildcard (p. 175)

Consider the following code: phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') mo = phoneNumRegex.search('123-456-7890') What will print(mo.group(1)) return?

'123'

Consider the following code: phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') mo = phoneNumRegex.search('123-456-7890') What will print(mo.group(0)) return?

'123-456-7890'

Consider the following code: phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') mo = phoneNumRegex.search('123-456-7890') What will print(mo.group(2)) return?

'456-7890'

The __________ character flags the group in parenthesis that precedes it as an optional part of the pattern.

? (r'Bat(wo)?man') can be used to search for Batman or woman. (p. 168-9)

Which regex is a shorter version of regex \d\d\d-\d\d\d-d\d\d\? A. {\d3} - {\d3} - {\d4} B. \d{3} - \d{3} - \d{4} C. {3}\d - {3}\d - {4}\d D. \d{3} - {3} - {4}

B. \d{3} - \d{3} - \d{4} (p. 164)

The method that will return the strings of every match in the searched string. A. group() B. findall() C. search() D. compile()

B. findall() (p. 171-2)

Which regex would match the phone number 555-555-5555? A. \d\d\d\d\d\d\d\d\d\d B. \d\d\d.\d\d\d.\d\d\d\d C. (\d\d\d) \d\d\d-\d\d\d D. \d\d\d-\d\d\d-d\d\d\

D. \d\d\d-\d\d\d-d\d\d\ (p. 164)

By putting an r before the first quote of a string value, you can mark the string as a(n) , which does not escape characters. A.Match Object B. newline character C. Regex object D. raw string

D. raw string

The __________ will match anything and everything up to the first newline character.

Dot-Star (.*) (p. 175-176)

The 4 steps to using regular expression are:

1. Import regex module with "import re" 2. Create a Regex object with the re.compile() function 3. Pass the string you want to search into Regex's search() method to return a match object (mo). 4. Call the match object's group() method: mo.group()

What does this error message indicate? NameError: name 're' is not defined

A failure to import re module (p. 165)

Which search will match an object with haRegex = re.compile(r'(Ha){3}')? A. mo1= haRegex.search('HaHaHa) B. mo2 = haRegex.search('Ha') C. Neither D. Both

A. mo1= haRegex.search('HaHaHa)

True or False: Python's regular expressions (regex) are greedy by default, which means that in ambiguous situations they will match the longest string possible.

True (p. 171)

True or False: There is no character class shorthand for matching only letters.

True (p. 173)

In regular expressions, __________ stands for a digit character or any single numeral from 0-9.

\d (p. 164)

The (r'^\D$') regular expression string matches strings that BEGIN and END with __________.

any character that is NOT a numeric digit 0-9 (p. 175)

If you need to match an actual ?, *, or + precede it with a __________.

backslash (\)

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 __________.

braces { } (p. 170)

In order to use regex functions in python you first must ___________ the __________ module.

import re

When used on a regex without groups (re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') the findall() method returns matches in a __________. When used on a regex with groups in parentheses (re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') regex returns matches as __________.

list list of tuples

Regular expressions allow you to specify a pattern of __________ to search for.

text (p. 162)


Kaugnay na mga set ng pag-aaral

Chapter 52: Care of the Patient with a Sensory Disorder

View Set

Review: Principles of Ecology - Food Chains

View Set

Prep U Porth chapter 26 blow flow & pressure

View Set

Chapter 15, Section 1: Two Revolutions in Russia

View Set

Exam 2 Pharmacology for Nurses (13, 48-50, 44-47, 39-40)

View Set