Happy Challenge: 10 Python Regex Questions

10 questions about Python regex

1. What is the module used for handling regular expressions in Python?

A. re

B. regex

C. pyregex

D. regexp

Answer: A Explanation: The module used for handling regular expressions is the re module.

Click to show the answer

2. In the following code snippet, what is the purpose of the re.compile() function?

import re

pattern = r"\d+"
regex = re.compile(pattern)

A. Compile a regular expression

B. Execute a regular expression

C. Match a regular expression

D. Replace a regular expression

Answer: A Explanation: The re.compile() function is used to compile a regular expression.

Click to show the answer

3. In a regular expression, what does the character set [abc] mean?

A. Match any one of the characters a, b, or c

B. Match one a character, one b character, and one c character

C. Match strings that start with a, b, or c

D. Match strings that end with a, b, or c

Answer: A Explanation: The character set [abc] means to match any one of the characters a, b, or c.

Click to show the answer

4. In a regular expression, what does the metacharacter . mean?

A. Match any character

B. Match one or more characters

C. Match zero or more characters

D. Match zero or one character

Answer: A Explanation: The metacharacter . means to match any character.

Click to show the answer

5. In the following code snippet, what is the purpose of the re.findall() function?

import re

text = "The quick brown fox jumps over the lazy dog."
pattern = r"\w+"
matches = re.findall(pattern, text)
print(matches)

A. Find all matching substrings

B. Find the first matching substring

C. Replace all matching substrings

D. Replace the first matching substring

Answer: A Explanation: The re.findall() function is used to find all matching substrings.

Click to show the answer

6. In a regular expression, what does the quantifier + mean?

A. Match one or more characters

B. Match zero or more characters

C. Match zero or one character

D. Match one character

Answer: A Explanation: The quantifier + means to match one or more characters.

Click to show the answer

7. In the following code snippet, what is the purpose of the re.sub() function?

import re

text = "Hello, World!"
pattern = r"\bWorld\b"
new_text = re.sub(pattern, "Python", text)
print(new_text)

A. Find all matching substrings

B. Find the first matching substring

C. Replace all matching substrings

D. Replace the first matching substring

Answer: C Explanation: The re.sub() function is used to replace all matching substrings.

Click to show the answer

8. In a regular expression, what is the purpose of grouping?

A. To divide the regular expression into multiple parts

B. To limit the matching scope of the regular expression

C. To repeat matching of specified characters or substrings

D. To extract or group matching results

Answer: D Explanation: The purpose of grouping is to extract or group matching results.

Click to show the answer

9. In the following code snippet, what is the purpose of the re.match() function?

import re

text = "The quick brown fox jumps over the lazy dog."
pattern = r"\w+"
match = re.match(pattern, text)
print(match.group())

A. Find all matching substrings

B. Find the first matching substring

C. Replace all matching substrings

D. Replace the first matching substring

Answer: B Explanation: The re.match() function is used to find the first matching substring.

Click to show the answer

10. In a regular expression, what is the purpose of a zero-width assertion?

A. Match specified characters or substrings

B. Check the text before or after the matching string

C. Repeat matching of specified characters or substrings

D. Extract or group matching results

Answer: B Explanation: The purpose of a zero-width assertion is to check the text before or after the matching string, but not include that text in the matching result.

Click to show the answer

 Written at the end

Welcome everyone to add me on WeChat (search python2818 or scan the QR code below). I will update my daily thoughts in my Moments, sharing entrepreneurial insights and experiences. You can observe my Moments, but I generally do not chat casually.

When adding, please note the message "888", and I will also send everyone a quality Python resource! Due to the large number of friends added, the approval may be slow, please bear with me!

Scan the QR code above to contact me

Leave a Comment