简体   繁体   中英

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. However, PO boxes usually include numbers as well, like "PO Box 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:

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.')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM