Python Regular Expression: Q&A On Regular Expression

Python Regular Expression. Regular Expression related Multiple Choice Questions in Python Programming.

Python Regular Expression

 

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’]

 

What will be the output of the following code ??

re.escape(‘new**world’)

a) ‘new world’
b) ‘new\\*\\*world’
c) ‘**’
d) ‘new’, ‘*’, ‘*’, ‘world’

 

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

 

The function re.error raises an exception if a particular string contains no match for the given pattern ??

a) True
b) False

Python Regular Expression

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

 

What will be the output of the following Python ??

re.sub(‘morning’, ‘evening’, ‘good morning’)

a) ‘good evening’
b) ‘good’
c) ‘morning’
d) ‘evening’

 

What is the output ??

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

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

 

What is the output of the following code ??

re.split(‘[a-c]’, ‘0a3B6’, re.I)

a) Error
b) [‘a’, ‘B’]
c) [‘0’, ‘3B6’]
d) [‘a’]

 

Python Regular Expression

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

 

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

re.compile(‘hello’, re.X)

a) [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]
b) re.compile(‘hello’, re.VERBOSE)
c) Error
d) Junk value

 

What will be the output of the following ??

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

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

 

Predict the output of the following Python code ??

a=re.compile(‘[0-9]+’)
a.findall(‘7 apples and 3 mangoes’)

a) [‘apples’ ‘and’ ‘mangoes’]
b) (7, 4)
c) [‘7’, ‘4’]
d) Error

 

Python Regular Expression

What will be the output of the following ??

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

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

 

What will be the output of the following Python ??

re.split(r’\s+’, ‘Chrome is better than explorer’, maxspilt=3)

a) [‘Chrome’, ‘is’, ‘better’, ‘than’, ‘explorer’]
b) [‘Chrome’, ‘is’, ‘better’, ‘than explorer’]
c) (‘Chrome’, ‘is’, ‘better’, ‘than explorer’)
d) ‘Chrome is better’ ‘than explorer’

 

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

Read More

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!