+ 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 RĂ©ponses
+ 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.