So im trying to read phone numbers from a file but i cant get it to handle numbers if I add extra numbers to the end EX: (123) 456-7890 is good but (123) 456-7890123 also goes through. How can I check for extra numbers at the end.
import re # Import Real Expressions
def isValid(s):
Filter1 = re.compile("[0-9]{3}\-[0-9]{3}\-[0-9]{4}") #Test for format xxx-xxx-xxxx
return Filter1.match(s) #return true if matches format
def isValid2(s):
Filter2 = re.compile("\([0-9]{3}\) [0-9]{3}\-[0-9]{4}") #Test for format (xxx) xxx-xxxx
return Filter2.match(s)# return true if matches format
def findValidPhone():
filename = "input1.txt" #delcare filename
with open(filename,"r") as inFile: #openfile
for line in inFile: #for all the lines in the file
s = line # store the line as a variable
# print(s)
if ( isValid(s)): #run tests using function isValid if true print number
print(s)
elif(isValid2(s)): #run test using function isValid2 if true print number
print(s)
else: # print invalid number if an invalid number is found in the file
print("Invalid Number")
inFile.close() #close the file
findValidPhone() #function call