邮政编码的正则表达式 [英] regex for zip-code

查看:62
本文介绍了邮政编码的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
什么是终极邮政编码和邮政编码?

我需要正则表达式,它可以满足我对邮政编码的所有三个条件.例如-

I need Regex which can satisfy all my three condtions for zip-code. E.g-

  1. 12345
  2. 12345-6789
  3. 12345 1234

任何指示和建议将不胜感激.谢谢!

Any pointers and suggestion would be much appreciated. Thanks !

推荐答案

^\d{5}(?:[-\s]\d{4})?$

  • ^ = 字符串的开始.
  • \d{5} = 匹配 5 个数字(对于条件 1、2、3)
  • (?:...) = 分组
  • [-\s] = 匹配空格(条件 3)或连字符(条件 2)
  • \d{4} = 匹配 4 位数字(条件 2、3)
  • ...? = 它之前的模式是可选的(对于条件 1)
  • $ = 字符串结束.
    • ^ = Start of the string.
    • \d{5} = Match 5 digits (for condition 1, 2, 3)
    • (?:…) = Grouping
    • [-\s] = Match a space (for condition 3) or a hyphen (for condition 2)
    • \d{4} = Match 4 digits (for condition 2, 3)
    • …? = The pattern before it is optional (for condition 1)
    • $ = End of the string.
    • 这篇关于邮政编码的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆