当前位置: 首页 > news >正文

Java 正则表达式重复匹配篇

重复匹配

  • * 可以匹配任意个字符,包括0个字符。
  • + 可以匹配至少一个字符。
  • ? 可以匹配0个或一个字符。
  • {n} 可以精确指定 n 个字符。
  • {n,m} 可以精确匹配 n-m 个字符。你可以是 0 。

匹配任意个字符

匹配 D 开头,后面是任意数字的字符,

        String regexU1 = "D\\d*";System.out.println("DD".matches(regexU1));// falseSystem.out.println("D1".matches(regexU1));// trueSystem.out.println("D202".matches(regexU1));// trueSystem.out.println("D88888888".matches(regexU1));// true

匹配至少一个字符

匹配 D 开头,后面至少是一位数字的字符,

        String regexU2 = "D\\d+";System.out.println("D".matches(regexU2));// falseSystem.out.println("D1".matches(regexU2));// trueSystem.out.println("D202".matches(regexU2));// trueSystem.out.println("D88888888".matches(regexU2));// true

匹配 0 个或一个字符

匹配 D 开头,后面是 0个或一个数字的字符,

        String regexU3 = "D\\d?";System.out.println("D".matches(regexU3));// trueSystem.out.println("D1".matches(regexU3));// trueSystem.out.println("D22".matches(regexU3));// falseSystem.out.println("D88888888".matches(regexU3));// false

匹配 n 个字符

匹配 D 开头,后面 3 个数字的字符,

        String regexU4 = "D\\d{3}";System.out.println("D".matches(regexU4));// falseSystem.out.println("D1".matches(regexU4));// falseSystem.out.println("D22".matches(regexU4));// falseSystem.out.println("D301".matches(regexU4));// trueSystem.out.println("D3004".matches(regexU4));// false

匹配 n-m 个字符

匹配 D 开头,后面是 3-5 位数字的字符,

        String regexU5 = "D\\d{3,5}";System.out.println("D".matches(regexU5));// falseSystem.out.println("D1".matches(regexU5));// falseSystem.out.println("D22".matches(regexU5));// falseSystem.out.println("D333".matches(regexU5));// trueSystem.out.println("D4000".matches(regexU5));// trueSystem.out.println("D55555".matches(regexU5));// trueSystem.out.println("D666666".matches(regexU5));// false
http://www.lryc.cn/news/217828.html

相关文章:

  • 0009Java安卓程序设计-ssm基于android手机设计并实现在线点单系统APP
  • react_14
  • 批量导出 PPT的备注到一个txt文本中
  • 文本内容转换成语音播放的工具:Speech Mac
  • 运维知识点-MySQL从小白到入土
  • 【蓝桥杯基础题】门牌制作
  • MyBatis底层原理(小白版本)
  • 水经微图Web版从入门到精通
  • IntelliJ IDEA 2023 最新版如何试用?IntelliJ IDEA 2023最新版试用方法及验证ja-netfilter配置成功提示
  • LeetCode541. Reverse String II
  • ios原生分享
  • 【Ubuntu】安装chrome之后无法启动
  • 顺序栈练习
  • 安全与HTTP协议:为何明文传输数据成为争议焦点?
  • 【笔记】excel怎么把汉字转换成拼音
  • opencv官网文档学习
  • Android性能优化--Perfetto用SQL性能分析
  • NLP之Bert实现文本分类
  • Pytorch从零开始实战08
  • docker部署Jenkins(Jenkins+Gitlab+Maven实现CI/CD)
  • mapbox使用marker创建html点位信息
  • 项目构建工具maven的基本配置
  • 超详细docker学习笔记
  • Adobe acrobat 11.0版本 pdf阅读器修改背景颜色方法
  • HCIA数据通信——路由协议
  • 十种常见典型算法
  • python-列表推导式、生成器表达式
  • NLP 模型中的偏差和公平性检测
  • YUV图像格式详解
  • 软考高项-质量管理措施