0

My python program is giving unexpected results within the regular expressions functions, when I enter a number plate for recognition, it tells me it's invalid, although it is valid and I don't know why?

I would be grateful if you could tell me what's wrong and give a possible solution, as this is a very important homework assignment.

#Start
#04/02/2016
bad=[]#initialise bad list
data="Y"#initialise value to prevent infinite loop
standardNumberPlateObj=""
NumPlate=""
import re#import re functions
import pickle#import pickle function

print ("Welcome to the number plate recognition program.\n\n")
choice=input("Press 1 to input number plates and save\nPress 2 to read binary number plate file: ")
if choice == "1":
        while data=="Y":# while loop
            NumPlate = input("Input Registration number in format (XX01 XXX) *With a space at the end!*:\n\n") #user input for numberplate
            standardNumberPlateObj=re.match(r'\w\w\d\d\s\w\w\w\s', NumPlate, re.M|re.I)#validate that numberplate is valid

            if standardNumberPlateObj:
                print("Verification Success")
                data=input(str("Would you like to continue? (Y/N):"))

            else:
                print("Verification Failed")
                bad.append(NumPlate)#add numberplate to bad list
                data=input(str("Would you like to continue? (Y/N):"))#ask user to continue


        while data=="N":#saving number plates to file if user enters N
             f = open("reg.dat", "wb")
             pickle.dump(bad, f)
             f.close()
             print ("\nRestart the program to read binary file!")#ending message
             break

elif choice == "2":
             print ("\nBad number plates:\n\n")
             f=open("reg.dat", "rb")
             Registrations = pickle.load(f)
             print(Registrations)
             f.close()

else:
             print ("Please enter a valid choice!")

print ("\n END of program!")
Kappa145
  • 1
  • 1

1 Answers1

0

It's not really possible to tell without an example input and the expected and the actual result.

But judging from the expression \w\w\d\d\s\w\w\w\s and your example in the prompt (XX01 XXX), I'd say that your regular expression is expecting a space in the end, while your input doesn't provide one.

mastov
  • 2,942
  • 1
  • 16
  • 33