+ 1

I have a question about regular expressions and need a little help in my code

It's a phone number validator. The should contain exactly eight numbers and should start with 1,8 or 9. Here's my code import re pattern = r"[1-9] number = input match = re.match(pattern, number) if match: print("Valid") else: print("Invalid") Can anyone please tell me how to make my start with 1 or 8 or 9

21st Jul 2021, 10:04 PM
Philippe Ezekiel
Philippe Ezekiel - avatar
3 ответов
+ 3
to validate that number starts with 1,8 or 9 you can try ro put it like this pattern = r"(1|8|9)[0-9]{7}
quot;
21st Jul 2021, 10:29 PM
Ali Al Dahneem
Ali Al Dahneem - avatar
+ 1
Philippe Here's a shorter pattern: r"[189]\d{7}
quot; # Hope this helps
22nd Jul 2021, 5:10 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
22nd Jul 2021, 8:20 AM
Ali Al Dahneem
Ali Al Dahneem - avatar