我有一个关于python正则表达式的问题。我正试图用flask restful制作一个注册页面,我正在努力将邮政编码与不同的国家匹配。E.g荷兰邮政编码:1234AB,比利时邮政编码:1234。我想要的是:如果选择荷兰,邮政编码必须有4位数字和2个字符。如果选择比利时,则邮政编码必须只有4位数字。
con = sqlite3.connect("db/webshop.db")
cur = con.cursor()
split_string = args['email'].split("@",1)
substring = split_string[0]
if len(args['password']) < 8:
print("Make sure your password is at least 8 letters")
return
elif re.search('[0-9]',args['password']) is None:
print("Make sure your password has a number in it")
return
elif re.search('[A-Z]',args['password']) is None:
print("Make sure your password has a capital letter in it")
return
elif len(substring) <= 5:
print("Make sure your email has 5 letter in it")
return
elif args['email'].find('@')==-1:
print("Make sure your email has @ letter in it")
return
elif args['email'].find('.')==-1:
print("Make sure your email has . letter in it")
return
elif args['country'] == '1':
if re.match(r'd[0-9]{4}', args['zipcode']):
cur.execute("INSERT INTO users (email,password,firstname,infix,lastname,street,housenumber,zipcode,city,newsletter,userrole_id,country_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)", (args['email'],args['password'],args['firstname'],args['infix'],args['lastname'],args['street'],args['housenumber'],args['zipcode'],args['city'],args['newsletter'],'1',args['country']))
con.commit()
else:
print("zipcode is not matching with given arguments")
return
您可以尝试以下操作:
belgium = '1' # assuming that belgium is '1'
if args['country'] == belgium:
zip_code_regexp = "^[0-9]{4}$"
else:
zip_code_regexp = "^[0-9]{4}[A-Z]{2}$"
if re.match(zip_code_regexp, args['zipcode']):
# ...
else:
# ...