doute7910 2013-08-14 16:10
浏览 40

RegEx英语和澳大利亚邮政编码

Could anyone point me in the right direction for a regular expression to validate an English postcode and Australian Postcode

English Eg: AA11 1AA Australian: 1111

My current expression to validate an English Postcode is:

$PostCodeRegex = '~^(GIR 0AA)|(TDCU 1ZZ)|(ASCN 1ZZ)|(BIQQ 1ZZ)|(BBND 1ZZ)'
.'|(FIQQ 1ZZ)|(PCRN 1ZZ)|(STHL 1ZZ)|(SIQQ 1ZZ)|(TKCA 1ZZ)'
. '|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]'
. '|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))'
. '|[0-9][A-HJKS-UW])\s?[0-9][ABD-HJLNP-UW-Z]{2}$~i';
  • 写回答

2条回答 默认 最新

  • douqu2712 2013-08-14 17:30
    关注

    You could've google it a bit, I found plenty of answers myself...

    I'm not sure about you UK regex above since it doesnt work for me, so I tried this one:

    ^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$
    
    Source: http://stackoverflow.com/a/164992/1519058
    

    And googling, I found this regex for australian postecodes:

    ^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$
    
    Source: http://www.etl-tools.com/regular-expressions/is-australian-post-code.html
    

    So now you just concatenate both using a "|", and it might gives you what you want:

    ^([A-PR-UWYZ0-9][A-HK-Y0-9][AEHMNPRTVXY0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}|GIR 0AA)$|^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$
    

    You can try it HERE

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么