Regular expressions · 10 / 15 Exercise Medium

Phone numbers

Objective

Create a Python program that validates mobile phone numbers with a pattern that includes an optional part.

Instructions

  • Read an integer n and then n phone numbers, one per line.
  • A phone number is valid if it has an optional prefix +34 (with the space) followed by 9 digits, the first of which is a 6 or a 7. The pattern is r"(\+34 )?[67]\d{8}".
  • Use re.fullmatch.
  • Show Valid or Not valid for each phone number.
Input
4
600123456
+34 700123456
500123456
+34600123456
Output
Valid
Valid
Not valid
Not valid

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise