Chapter 7: Pattern Matching with Regular Expressions

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan 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.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 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()

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.

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

A failure to import re module

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)

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}

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

B. findall()

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\

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 (.*)

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')

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

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

True

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

\d

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

any character that is NOT a numeric digit 0-9

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 { }

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

caret (^)

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

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

dollar sign($)

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.

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

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

numeric digit 0-9

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

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 (+)

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

question mark (?)

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

question mark (?)

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 (*)

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

text

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

wildcard


Set pelajaran terkait

Vocabulary Workshop Level E Unit 1 SYNONYMS & ANTONYMS, Vocab Level E Unit 1 Completing the Sentence

View Set

BUSI 520 Chapter 9: Enhancing Decision Making with Solver

View Set

Foundational Principles of Social Justice

View Set