LearnFrenzy
Sign in Take a free test

Regular Expressions · Predict the Output · Question 11

^ with and without MULTILINE Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

What is the output of the following Python program?

main.py
import re
text = "one two\nthree four"
print(re.findall(r"^\w+", text), re.findall(r"^\w+", text, re.M), re.findall(r"\w+$", text, re.M))
  • A ['one', 'three'] ['one', 'three'] ['four']
  • B ['one'] ['one'] ['four']
  • C ['one'] ['one', 'three'] ['two', 'four'] Correct
  • D ['one'] ['one', 'three'] ['four']
Correct Answer: Option C

Without re.M, ^ matches only at the very start. With it, ^ and $ match at every line start and end.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links