簡體   English   中英

檢查列表中是否有任何字符串實例

[英]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.')

如果將PO Box插入值字段,則嘗試引發錯誤。 但是,郵政信箱通常也包括數字,例如“郵政信箱1167”。 我該如何編寫驗證以僅檢查字符串中的那些實例,而忽略數字。

怎么樣

if "pobox" in str(Resp_Info.r3.val).lower().replace(".", "").replace(" ", ""):
    print "err"

使用正則表達式:

import re
if "pobox" in re.sub('\W+','', "PO Box #1234" ).lower():
    print "err"

您可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM