Python Questions and Answers – Regular Expressions – 1

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

1. The character Dot (that is, ‘.’) in the default mode, matches any character other than _____________
a) caret
b) ampersand
c) percentage symbol
d) newline
View Answer

Answer: d
Explanation: The character Dot (that is, ‘,’) in the default mode, matches any character other than newline. If DOTALL flag is used, then it matches any character other than newline.

2. The expression a{5} will match _____________ characters with the previous regular expression.
a) 5 or less
b) exactly 5
c) 5 or more
d) exactly 4
View Answer

Answer: b
Explanation: The character {m} is used to match exactly m characters to the previous regular expression. Hence the expression a{5} will match exactly 5 characters and not less than that.

3. ________ matches the start of the string.
________ matches the end of the string.
a) ‘^’, ‘$’
b) ‘$’, ‘^’
c) ‘$’, ‘?’
d) ‘?’, ‘^’
View Answer

Answer: a
Explanation: ‘^’ (carat) matches the start of the string.
‘$’ (dollar sign) matches the end of the string.
advertisement
advertisement

4. Which of the following will result in an error?
a)

>>> p = re.compile("d")
>>> p.search("door")

b) >>> p = re.escape(‘hello’)
c) >>> p = re.subn()
d) >>> p = re.purge()
View Answer

Answer: c
Explanation: The function re.subn() will result in an error. This is because subn() requires 3 positional arguments while we have entered none.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

re.split('\W+', 'Hello, hello, hello.')

a) [‘Hello’, ‘hello’, ‘hello.’]
b) [‘Hello, ‘hello’, ‘hello’]
c) [‘Hello’, ‘hello’, ‘hello’, ‘.’]
d) [‘Hello’, ‘hello’, ‘hello’, ”]
View Answer

Answer: d
Explanation: In the code shown above, the function split() splits the string based on the pattern given as an argument in the parenthesis. Note: split will never split a string on an empty pattern match. Hence the output of this code is: [‘Hello’, ‘hello’, ‘hello’, ”].
advertisement

6. What will be the output of the following Python function?

advertisement
re.findall("hello world", "hello", 1)

a) [“hello”]
b) [ ]
c) hello
d) hello world
View Answer

Answer: b
Explanation: The function findall returns the word matched if and only if both the pattern and the string match completely, that is, they are exactly the same. Observe the example shown below:
>>> re.findall(“hello”, “hello”, 1) The output is: [‘hello’] Hence the output of the code shown in this question is [].

7. Choose the function whose output can be: <_sre.SRE_Match object; span=(4, 8), match=’aaaa’>.
a) >>> re.search(‘aaaa’, “alohaaaa”, 0)
b) >>> re.match(‘aaaa’, “alohaaaa”, 0)
c) >>> re.match(‘aaa’, “alohaaa”, 0)
d) >>> re.search(‘aaa’, “alohaaa”, 0)
View Answer

Answer: a
Explanation: The output shown above is that of a search function, whose pattern is ‘aaaa’ and the string is that of 8 characters. The only option which matches all these criteria is:
>>> re.search(‘aaaa’, “alohaaaa”, 0)

8. Which of the following functions clears the regular expression cache?
a) re.sub()
b) re.pos()
c) re.purge()
d) re.subn()
View Answer

Answer: c
Explanation: The function which clears the regular expression cache is re.purge(). Note that this function takes zero positional arguments.

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

import re
re.ASCII

a) 8
b) 32
c) 64
d) 256
View Answer

Answer: d
Explanation: The expression re.ASCII returns the total number of ASCII characters that are present, that is 256. This can also be abbreviated as re.A, which results in the same output (that is, 256).

10. Which of the following functions results in case insensitive matching?
a) re.A
b) re.U
c) re.I
d) re.X
View Answer

Answer: c
Explanation: The function re.I (that is, re.IGNORECASE) results in case-insensitive matching. That is, expressions such as [A-Z] will match lowercase characters too.

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.