[英]Checking if any instance of string in list
POarray = ['P.O. Box', 'P.O. BOX', 'P.O. box', 'p.o. box', 'PO Box', 'PO BOX', 'PO box', 'po box', 'P.O Box', 'P.O BOX', 'P.O box', 'p.o box']
if Resp_Info.r3.val in POarray:
error('Sorry, but we cannot except PO boxes.')
I'm trying to raise an error if a PO Box is inserted into a value field. 如果将PO Box插入值字段,则尝试引发错误。 However, PO boxes usually include numbers as well, like "PO Box 1167". 但是,邮政信箱通常也包括数字,例如“邮政信箱1167”。 How do I write my validation to check just for those instances in my string, and ignore the numbers. 我该如何编写验证以仅检查字符串中的那些实例,而忽略数字。
How about 怎么样
if "pobox" in str(Resp_Info.r3.val).lower().replace(".", "").replace(" ", ""):
print "err"
Using Regex: 使用正则表达式:
import re
if "pobox" in re.sub('\W+','', "PO Box #1234" ).lower():
print "err"
You can use re
module: 您可以使用re
模块:
import re
pattern = re.compile('p\.?o\.?\sbox', re.IGNORECASE)
if re.search(pattern, Resp_Info.r3.val):
error('Sorry, but we cannot except PO boxes.')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.