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

Java Matcher对象中find()与matches()的区别

`find()`:字符串某个部分匹配上正则表达式就会返回true
`matches()`:字符串整体匹配上正则表达式才返回true


find():字符串某个部分匹配上正则表达式就会返回true

Pattern compile = Pattern.compile("[WASD][0-9]{1,2}");
System.out.println(compile.matcher("A1").find());  //true
System.out.println(compile.matcher("A12").find());  //true
System.out.println(compile.matcher("E12").find());  //false
System.out.println(compile.matcher("AE12").find());  //false
System.out.println(compile.matcher("AEA12").find());  //true
System.out.println(compile.matcher("AEA1A2").find());  //true
System.out.println(compile.matcher("AEA12444").find());  //true


matches():字符串整体匹配上正则表达式才返回true

Pattern compile = Pattern.compile("[WASD][0-9]{1,2}");
System.out.println(compile.matcher("A122").matches());  //false
System.out.println(compile.matcher("AA12").matches());  //false
System.out.println(compile.matcher("A12").matches());  //true
System.out.println(compile.matcher("A1").matches());  //true
System.out.println(compile.matcher("AA1").matches());  //false
System.out.println(compile.matcher("AA12").matches());  //falseSystem.out.println("A122".matches("[WASD][0-9]{1,2}")); // false
System.out.println("AA12".matches("[WASD][0-9]{1,2}")); // false
System.out.println("A12".matches("[WASD][0-9]{1,2}")); // true
System.out.println("A1".matches("[WASD][0-9]{1,2}")); // true
System.out.println("A1".matches("[WASD]{1,2}[0-9]{1,2}")); // true
System.out.println("A12".matches("[WASD]{1,2}[0-9]{1,2}")); // true
System.out.println("AA1".matches("[WASD]{1,2}[0-9]{1,2}")); // true
System.out.println("AA12".matches("[WASD]{1,2}[0-9]{1,2}")); // true
http://www.dtcms.com/a/307493.html

相关文章:

  • sqli-labs:Less-15关卡详细解析
  • 10.C 语言内存划分,static,字符串
  • MFC CChartCtrl编程
  • 逻辑回归的应用
  • 【人工智能】当AI智能体遇上安全与伦理:一场技术与人性的对话
  • 3DXML 转换为 UG 的技术指南及迪威模型网在线转换推荐
  • arm架构系统打包qt程序--麒麟操作系统为例
  • 递归混合架构(MoR)在医疗领域的发展应用能力探析
  • 网络编程(一)TCP编程和UDP编程
  • Kubernetes集群中滚动更新失败与资源配置错误的深度解析及应对策略
  • 机器学习03——数据与算法初步2
  • Git之本地仓库管理
  • 第一篇:【Python-geemap教程(三)上】3D地形渲染与Landsat NDVI计算
  • 学习 java web 简单监听器
  • 《能碳宝》AI辅助开发系统方案
  • ES 工业网关:比德国更适配,比美国更易用
  • 编程语言Java——核心技术篇(六)解剖反射:性能的代价还是灵活性的福音?
  • Ubuntu/Debian 搭建 Nginx RTMP 服务器全攻略
  • 使用的IDE没有内置MCP客户端怎么办?
  • [源力觉醒 创作者计划]_文心4.5开源测评:国产大模型的技术突破与多维度能力解析
  • 数据库中使用SQL作分组处理01(简单分组)
  • Web3.0 和 Web2.0 生态系统比较分析:差异在哪里?
  • Web3:在 VSCode 中使用 Vue 前端与已部署的 Solidity 智能合约进行交互
  • Kotlin -> 普通Lambda vs 挂起Lambda
  • Astra主题WooCommerce如何添加可变产品Astra variation product
  • tplink er2260t配置vlan透传iptv
  • python学智能算法(二十九)|SVM-拉格朗日函数求解中-KKT条件理解
  • 数据结构: 双向列表
  • 银河麒麟桌面操作系统:自定义截图快捷键操作指南
  • NXP i.MX8MP GPU 与核心库全景解析