java - 邮政编码的正则表达式模式
问题描述
我正在尝试为 4 位连字符或由 3 位数字连续的空格编写正则表达式模式,例如
1234 123
1234-123
我已经尝试过了,但它不符合要求
^\\d{4}[-]{0,1}\\d{3}$
解决方案
Just include a space in the character class. If the hyphen or space is optional, add a ?
after it.
^\d{4}[ -]\d{3}$
Note that when using String#matches
, you do not need to include the start (^
) and end ($
) anchors.
System.out.println("1234-123".matches("\\d{4}[ -]\\d{3}")); // true
推荐阅读
- gensim - doc2vec(gensim) infer_vector 需要窗口大小的填充句吗?
- database - 如何检查oracle数据库中的sql_trace?
- node.js - Graphql 服务器 + NodeJS,使用 Redis 将数据存储在缓存中
- jquery - 请求的资源上不存在“Access-Control-Allow-Origin”标头,响应的 HTTP 状态代码为 401
- angular - 不活动后Angular 4重定向
- wordpress - 是否可以使用负载平衡和自动缩放在 aws ec2 中创建 wordpress 站点
- ruby-on-rails - 如何将数据写入excel文件
- jenkins - 设置 Sonar-Gerrit 插件:无法在 UserAtHost 上执行命令“gerrit review”
- java - Elasticsearch 使用 RestHighLevelClient 设置限制和偏移量
- c# - 使用 C# 在 Excel 中将单元格内容转换为字符串数据类型