Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2b. (8 pts) Recall that the sub (substitute) function in the re module is defined like def sub (re_pattern str, rep_func callable, text :

 

2b. (8 pts) Recall that the sub (substitute) function in the re module is defined like def sub (re_pattern str, rep_func callable, text : str) -> str: It searches text for substrings matching re_pattern; for every substring that it finds, it replaces it with the result of calling rep_func, supplying the resulting match object as its argument. For example re.sub (r"a\d", (lambda m "A"), "a5bcayza7") returns "AbcayzA": every occurrence of a followed by any digit is replaced by "A". We don't use or $ in this pattern, because the pattern can match anywhere in text. Here rep_func is a simple lambda that does not use the match object it is passed, but m is bound to the match object so the match object could have been used inside the lambda. We define a cluster of values as one or more possibly signed one-digit integers (including 0) that appear between < and >; two or more cluster values are separated by commas (,) and there are no spaces inside the cluster. For example is a cluster; so is . Note that the characters and > have no special meaning in RE. Define a pattern and rep_func below, so the call re. sub (pattern, rep_func, text) replaces every cluster in text by the sum of its values. For example re.sub (pattern, rep_func, 'I met and ) returns the string 'I met 3 and 4' pattern = def rep func (mo) -> str : #mo is the match object for each time pattern matches in text 2c. (1 pt) What result string is returned by the call re.sub (pattern, rep_func, 'answer = ).

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Statistics For Business And Economics

Authors: James T. McClave, P. George Benson, Terry T Sincich

12th Edition

032182623X, 978-0134189888, 134189884, 978-0321826237

More Books

Students also viewed these Programming questions

Question

What is statistics?

Answered: 1 week ago