+ 2
Python Regex
I am trying to match a phone number Where phone number start with 1 or 8 or 9 and total number of digits must be 8. What am i doing wrong? import re n=input() pattern=r'(1|8|9)[0-9]{7}' if(re.match(pattern,n)): print("Valid") else: print("Invalid")
7 odpowiedzi
+ 2
It’s likely you need to anchor your match to the end of the string using ‘$’. This avoids matches for inputs more than 8 numbers long. Try
pattern = re.match(r’(1|8|9)[0-9]{7}$’, n))
+ 3
Jan Markus I see :) it works fine like that.
+ 3
sid Inputs ‘123456789’ and ‘12345678hello’ are both returning ‘Valid’ which they are not.
+ 2
Jan Markus I don’t see any difference between your code and OP’s... 🤔
+ 2
Jan Markus, Russ Thank you for your time guyz.Really appreciated.
Stay Safe.
+ 1
Jan Markus no its not working
+ 1
Russ it was the '#x27;.now its passed
Thank you.