dtr87341 2018-10-18 18:46
浏览 91
已采纳

正则表达式验证地址字符串不包含邮政信箱模式

I'm using Laravel regex validation on the address field provided by users.

I found multiple posts that help to validate address string to be a valid PO Box like here or here, but I need to do the opposite and make sure the provided string is not a PO Box.

PS I don't need to do any address validation, just wanna make sure its anything but a PO Box.

I found this rule pretty useful since it checks for po box number to be present

/^\s*(.*((p|post)[-.\s]*(o|off|office)[-.\s]*(b|box|bin)[-.\s]*)|.*((p|post)[-.\s]*(o|off|office)[-.\s]*)|.*((p|post)[-.\s]*(b|box|bin)[-.\s]*)|(box|bin)[-.\s]*)(#|n|num|number)?\s*\d+/i

Can someone help me to create opposite expression for this one or anything similar?

I suspect I have to use negative lookahead ?!

  • 写回答

1条回答 默认 最新

  • douhui5529 2018-10-19 17:35
    关注

    Solved it by using regex negative lookahead ?!

    /^(?!.*(?:(.*((p|post)[-.\s]*(o|off|office)[-.\s]*(box|bin)[-.\s]*)|.*((p |post)[-.\s]*(box|bin)[-.\s]*)))).*$/i
    

    This rule does not require po box number to be present.

    Those are examples that will pass:

            "The Postal Road",
            "Box Hill",
            "123 Some Street",
            "Controller's Office",
            "pollo St.",
            "123 box canyon rd",
            "777 Post Oak Blvd",
            "PSC 477 Box 396",
            "RR 1 Box 1020",
            "Coop services",
            "65 Brook Ave Ste 1P",
            "pacific city",
            "orange county",
            "obox",
            "pbox",
            "party house",
            "p shorty",
            "something pacif pobo",
            "po helpkline box",
            "po ferrirs",
            "panther boxing",
            "box center",
            "post office",
            "Post",
            "porceline"
    

    Those will fail the validation

            "post office box",
            "POBOX",
            "po box",
            "POBox12234",
            "p o box",
            "P.O.Box",
            "HC73 P.O. Box 217",
            "P O Box125",
            "P. O. Box",
            "P.O. Box 123",
            "P.O. Box",
            "PO Box N",
            "PO Box",
            "PO-Box",
            "POBOX123",
            "Po Box",
            "Post Box 123",
            "Post Office Box 123",
            "Post Office Box",
            "p box",
            "p-o box",
            "p-o-box",
            "p.o box",
            "p.o. box",
            "p.o.-box",
            "po box 123",
            "po box",
            "po-box",
            "pobox",
            "pobox123",
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码