Home > Python > Quizzes > Python Programming Basics Practice Test: Python Regular Expressions
Python Programming Basics Practice Test: Python Regular Expressions
Fast practice, instant feedback. Timer auto-submits when time’s up.
Avg score: 0% Most missed: “________ matches the start of the string. | ________ matches the end of the stri…”
Regular expressions, often shortened to regex or regexp, are a sequence of characters that define a search pattern. They can be used to search, edit, or manipulate text. Python has a built-in package called re that supports regular expressions. Here are some of the basic metacharacters used in RegEx: ^: Checks if the string starts with a particular word or character $: Checks if the string ends with a particular word or character |: Used to check either/or condition Here are some examples of regular expressions: \d matches any single digit \w matches any alphanumeric character .... Show more
Python Programming Basics Practice Test: Python Regular Expressions
Time left 00:00
25 Questions

1. Which of the following pattern matching modifiers permits whitespace and comments inside the regular expression?
2. Which of the following special characters represents a comment (that is, the contents of the parenthesis are simply ignores)?
3. What will be the output of the following Python code?
m = re.search('a', 'The blue umbrella')
m.re.pattern
4. Which of the following functions returns a dictionary mapping group names to group numbers?
5. What will be the output of the following Python code?
re.match('sp(.*)am', 'spam')
6. What will be the output of the following Python code?
re.split('\W+', 'Hello, hello, hello.')
7. What will be the output of the following Python code?
sentence = 'horses are fast'
regex = re.compile('(?P\w+) (?P\w+) (?P\w+)')
matched = re.search(regex, sentence)
print(matched.group(2))
8. What will be the output of the following Python code?
s = 'welcome home'
m = re.match(r'(.*)(.*?)', s)
print(m.group())
9. The function of re.search is __________
10. The function of re.match is ____________
11. What will be the output of the following Python code?
w = re.compile('[A-Za-z]+')
w.findall('It will rain today')
12. What does the function re.match do?
13. The special character \B matches the empty string, but only when it is _____________
14. What will be the output of the following Python code?
re.escape('new**world')
15. What will be the output of the following Python code?
re.split('[a-c]', '0a3B6', re.I)
16. What will be the output of the following Python code?
sentence = 'horses are fast'
regex = re.compile('(?P\w+) (?P\w+) (?P\w+)')
matched = re.search(regex, sentence)
print(matched.groups())
17. The following Python code snippet results in an error.
c=re.compile(r'(\d+)(\[A-Z]+)([a-z]+)')
c.groupindex
18. Which of the following creates a pattern object?
19. Which of the following functions clears the regular expression cache?
20. What will be the output of the following Python code?
a = re.compile('0-9')
a.findall('3 trees')
21. Which of the following functions creates a Python object?
22. Which of the following statements regarding the output of the function re.match is incorrect?
23. What will be the output of the following Python code?
re.fullmatch('hello', 'hello world')
24. Which of the following functions results in case insensitive matching?
25. Choose the function whose output can be: <_sre.SRE_Match object; span=(4, 8), match=’aaaa’>.