Python Questions and Answers – Regular Expressions – 3

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

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

re.split(r'(n\d)=', 'n1=3.1, n2=5, n3=4.565')

a) Error
b) [”, ‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’]
c) [‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’]
d) [‘3.1, ‘, ‘5, ‘, ‘4.565’]
View Answer

Answer: b
Explanation: In the snippet of code shown above, we extract the numbers as a list of floating point values, including the initial empty string. The example shown above demonstrate how groups in the regular expression influence the result of re.split. Hence the output of the code shown above is:
[”, ‘n1’, ‘3.1, ‘, ‘n2’, ‘5, ‘, ‘n3’, ‘4.565’].
advertisement
advertisement

2. The function of re.search is __________
a) Matches a pattern at the start of the string
b) Matches a pattern at the end of the string
c) Matches a pattern from any part of a string
d) Such a function does not exist
View Answer

Answer: c
Explanation: The re module of Python consists of a function re.search. It’s function is to match a pattern from anywhere in a string.

3. Which of the following functions creates a Python object?
a) re.compile(str)
b) re.assemble(str)
c) re.regex(str)
d) re.create(str)
View Answer

Answer: a
Explanation: The function re.compile(srt) compiles a pattern of regular expression into an object of regular expression. Hence re.compile(str) is the only function from the above options which creates an object.

4. Which of the following pattern matching modifiers permits whitespace and comments inside the regular expression?
a) re.L
b) re.S
c) re.U
d) re.X
View Answer

Answer: d
Explanation: The modifier re.X allows whitespace and comments inside the regular expressions.

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

advertisement
s = 'welcome home'
m = re.match(r'(.*)(.*?)', s)
print(m.group())

a) (‘welcome’, ‘home’)
b) [‘welcome’, ‘home’]
c) welcome home
d) [‘welcome’ // ‘home’ ]
View Answer

Answer: c
Explanation: The code shown above shows the function re.match combined with the use of special characters. Hence the output of this code is: welcome home.
advertisement

6. The function of re.match is ____________
a) Error
b) Matches a pattern anywhere in the string
c) Matches a pattern at the end of the string
d) Matches a pattern at the start of the string
View Answer

Answer: d
Explanation: The function of re.match matches a pattern at the start of the string.

7. The special character \B matches the empty string, but only when it is _____________
a) at the beginning or end of a word
b) not at the beginning or end of a word
c) at the beginning of the word
d) at the end of the word
View Answer

Answer: b
Explanation: The special character \B matches the empty string, but only when it is not at the beginning or end of a word.

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

import re
s = "A new day"
m = re.match(r'(.*)(.*?)', s)
print(m.group(2))
 
print(m.group(0))

a)

No output
A new day

b)

No output
No output

c)

[‘A’, ‘new’, ‘day’]
(‘A’, ‘new’, ‘day’)

d)

Error
[‘A’, ‘new’, ‘day’]
View Answer
Answer: a
Explanation: The code shown above demonstrates the use of the function re.match, with different arguments given to the group method. Hence the first function does not return any output whereas the second function returns the output: A new day
 
 

9. Which of the following special characters matches a pattern only at the end of the string?
a) \B
b) \X
c) \Z
d) \A
View Answer

Answer: c
Explanation: \B matches a pattern which is not at the beginning or end of a string. \X refers to re.VERBOSE. \A matches a pattern only at the start of a string. \Z matches a pattern only at the end of a string.

10. The output of the following two Python codes are the same.

p = re.compile('hello')
r = p.match('hello everyone')
print(r.group(0))
 
r = re.match('hello', 'hello everyone')
print(r.group(0))

a) True
b) False
View Answer

Answer: a
Explanation: The two codes shown above are equivalent. Both of these codes result in the same output, that is: hello. Hence this statement is true.

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

re.match('sp(.*)am', 'spam')

a) <_sre.SRE_Match object; span=(1, 4), match=’spam’>
b) <_sre.SRE_Match object; span=(0, 4), match=’spam’>
c) No output
d) Error
View Answer

Answer: b
Explanation: The code shown above demonstrates the function re.match, combined with a special character. The output of the code shown is: <_sre.SRE_Match object; span=(0, 4), match=’spam’>

12. Which of the following special characters represents a comment (that is, the contents of the parenthesis are simply ignores)?
a) (?:…)
b) (?=…)
c) (?!…)
d) (?#…)
View Answer

Answer: d
Explanation: The special character (?#…) represent a comment, that is, the contents of the parenthesis are simply ignored.

13. Which of the codes shown below results in a match?
a) re.match(‘George(?=Washington)’, ‘George Washington’)
b) re.match(‘George(?=Washington)’, ‘George’)
c) re.match(‘George(?=Washington)’, ‘GeorgeWashington’)
d) re.match(‘George(?=Washington)’, ‘Georgewashington’)
View Answer

Answer: c
Explanation: The code shown above demonstrates the use of the function re.match, along with the special character ?=. This results in a match only when ‘George’ is immediately followed by ‘Washington’. Also, we have not used the module to ignore case. Hence the match is case-sensitive. Therefore the only option which results in a match is:
re.match(‘George(?=Washington)’, ‘GeorgeWashington’)

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.