Vidyalelo
Python · all questions

Regular Expressions in Python
practice.

Practice every MCQ with options. Use Show answers when you want the correct option and solution.

90

Questions

4/5

Page

Pick an option on a question to see the right answer and solution.

What is the output of the following code:
import re
result = re.sub(r's', '-', 'This is a test.')
print(result)

Select an option to see the answer and solution.

What will be the output of the following Python code?
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.group())

Select an option to see the answer and solution.

Which of the following functions does not accept any argument?

Select an option to see the answer and solution.

How can you match either of two patterns in regular expressions using the | operator?

Select an option to see the answer and solution.

What is the purpose of the re.search() function in Python?

Select an option to see the answer and solution.

What will be the output of the following Python code?
re.findall('good', 'good is good')
re.findall('good', 'bad is good')

Select an option to see the answer and solution.

How can you match the end of a string using regular expressions?

Select an option to see the answer and solution.

What will be the output of the following Python code?
re.fullmatch('hello', 'hello world')

Select an option to see the answer and solution.

What will be the output of the following Python code?
a=re.compile('[0-9]+')
a.findall('7 apples and 3 mangoes')

Select an option to see the answer and solution.

The function of re.match is . . . . . . . .

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

What will be the output of the following Python code?
s = 'welcome home'
m = re.match(r'(.*)(.*?)', s)
print(m.group())

Select an option to see the answer and solution.

What is the output of the following code:
import re
pattern = re.compile(r'(d+)s(w+)')
result = pattern.findall('There are 5 apples and 3 oranges.')
print(result)

Select an option to see the answer and solution.

The following Python code snippet results in an error.
c=re.compile(r'(\d+)(\[A-Z]+)([a-z]+)')
c.groupindex

Select an option to see the answer and solution.

What will be the output of the following Python code?
a = re.compile('0-9')
a.findall('3 trees')

Select an option to see the answer and solution.

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

Select an option to see the answer and solution.

Which of the following functions creates a Python object?

Select an option to see the answer and solution.

Which of the following special characters represents a comment (that is, the contents of the parenthesis are simply ignores)?

Select an option to see the answer and solution.

What will be the output of the following Python code?
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.groups())

Select an option to see the answer and solution.

Which of the following special characters matches a pattern only at the end of the string?

Select an option to see the answer and solution.