LearnFrenzy
Sign in Take a free test

Regular Expressions · Find the Error · Question 8

Bug: a 7-digit PIN is accepted Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

Indian PIN codes have exactly 6 digits, but "5600341" is reported as valid. Which statement correctly identifies the bug?

main.py
import re
for pin in ["560034", "5600341", "PIN560034"]:
    print(pin, "valid" if re.search(r"\d{6}", pin) else "invalid")
  • A \d{6} means at least 6 digits
  • B search finds 6 digits anywhere inside the string; validate the whole string with re.fullmatch(r"[1-9]\d{5}", pin) or anchors ^…$ Correct
  • C search ignores letters
  • D PIN codes must be ints
Correct Answer: Option B

Validation needs fullmatch or ^ and $ so that nothing extra is allowed.

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