我的python程序在正则表达式函数中给出了意想不到的结果,当我输入一个用于识别的号牌时,它告诉我它无效,虽然它是有效的但我不知道为什么?
如果你能告诉我什么是错的并给出一个可能的解决方案,我将不胜感激,因为这是一项非常重要的家庭作业。
#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!")
答案 0 :(得分:0)
如果没有示例输入以及预期和实际结果,这是不可能的。
但是从表达式\w\w\d\d\s\w\w\w\s
以及提示符(XX01 XXX
)中的示例来判断,我说你的正则表达式最终期望一个空格,而你的输入并没有&# 39;提供一个。