Python

Python

Regular Expressions has 10 Questions

Which module in Python supports regular expression?

re
regex
pyregex
none of the mentioned
Which of the following creates a pattern object?

re.create(str)
re.regex(str)
re.compile(str)
re.assemble(str)
What does the function re.match do?

matches a pattern at the start of the string
matches a pattern at any position in the string
such a function does not exist
none of the mentioned
What does the function re.search do?

matches a pattern at the start of the string
matches a pattern at any position in the string
such a function does not exist
none of the mentioned
What is the output of the following?
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.groups())

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

('we', 'are', 'humans')
(we, are, humans)
('we', 'humans')
'we are humans'
What is the output of the following?
sentence = 'we are humans'
matched = re.match(r'(.*) (.*?) (.*)', sentence)
print(matched.group(2))

'are'
'we'
'humans'
'we are humans'
What is the output of the following?
sentence = 'horses are fast'
regex = re.compile('(?P\w+) (?P\w+) (?P\w+)')
matched = re.search(regex, sentence)
print(matched.groupdict())

{'animal': 'horses', 'verb': 'are', 'adjective': 'fast'}
('horses', 'are', 'fast')
'horses are fast'
'are'
What is the output of the following?
sentence = 'horses are fast'
regex = re.compile('(?P\w+) (?P\w+) (?P\w+)')
matched = re.search(regex, sentence)
print(matched.groups())

{'animal': 'horses', 'verb': 'are', 'adjective': 'fast'}
('horses', 'are', 'fast')
'horses are fast'
'are'
What is the output of the following?
sentence = 'horses are fast'
regex = re.compile('(?P\w+) (?P\w+) (?P\w+)')
matched = re.search(regex, sentence)
print(matched.group(2))

{'animal': 'horses', 'verb': 'are', 'adjective': 'fast'}
('horses', 'are', 'fast')
'horses are fast'
'are'