Python Questions and Answers – Regular Expressions – 5

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Regular Expressions – 5”.

1. Which of the following functions returns a dictionary mapping group names to group numbers?
a) re.compile.group
b) re.compile.groupindex
c) re.compile.index
d) re.compile.indexgroup
View Answer

Answer: b
Explanation: The function re.compile.groupindex returns a dictionary mapping group names to group numbers.

2. Which of the following statements regarding the output of the function re.match is incorrect?
a) ‘pq*’ will match ‘pq’
b) ‘pq?’ matches ‘p’
c) ‘p{4}, q’ does not match ‘pppq’
d) ‘pq+’ matches ‘p’
View Answer

Answer: d
Explanation: All of the above statements are correct except that ‘pq+’ match ‘p’. ‘pq+’ will match ‘p’ followed by any non-zero number of q’s, but it will not match ‘p’.

3. The following Python code snippet results in an error.

advertisement
advertisement
c=re.compile(r'(\d+)(\[A-Z]+)([a-z]+)')
c.groupindex

a) True
b) False
View Answer

Answer: b
Explanation: In the code shown above, none of the group names match the group numbers. In such a case, no error is thrown. The output of the code is an empty dictionary, that is, {}.

4. Which of the following functions does not accept any argument?
a) re.purge
b) re.compile
c) re.findall
d) re.match
View Answer

Answer: a
Explanation: The function re.purge is used to clear the cache and it does not accept any arguments.

5. What will be the output of the following Python code?

advertisement
a = re.compile('0-9')
a.findall('3 trees')

a) []
b) [‘3’]
c) Error
d) [‘trees’]
View Answer

Answer: c
Explanation: The output of the code shown above is an empty list. This is due to the way the arguments have been passed to the function re.compile. Carefully read the code shown below in order to understand the correct syntax:
>>> a = re.compile(‘[0-9]’)
>>> a.findall(‘3 trees’)
[‘3’].
advertisement

6. Which of the following lines of code will not show a match?
a) >>> re.match(‘ab*’, ‘a’)
b) >>> re.match(‘ab*’, ‘ab’)
c) >>> re.match(‘ab*’, ‘abb’)
d) >>> re.match(‘ab*’, ‘ba’)
View Answer

Answer: d
Explanation: In the code shown above, ab* will match to ‘a’ or ‘ab’ or ‘a’ followed by any number of b’s. Hence the only line of code from the above options which does not result in a match is:
>>> re.match(‘ab*’, ‘ba’).

7. What will be the output of the following Python code?

m = re.search('a', 'The blue umbrella')
m.re.pattern

a) {}
b) ‘The blue umbrella’
c) ‘a’
d) No output
View Answer

Answer: c
Explanation: The PatternObject is used to produce the match. The real regular expression pattern string must be retrieved from the PatternObject’s pattern method. Hence the output of this code is: ‘a’.

8. What will be the output of the following Python code?

re.sub('Y', 'X', 'AAAAAA', count=2)

a) ‘YXAAAA’
b) (‘YXAAAA’)
c) (‘AAAAAA’)
d) ‘AAAAAA’
View Answer

Answer: d
Explanation: The code shown above demonstrates the function re.sub, which returns a string. The pattern specified is substituted in the string and returned. Hence the output of the code shown above is: ‘AAAAAA’.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.