当前位置: 首页 > 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.xdnf.cn/news/1218349.html

相关文章:

  • QT6 Python UI文件转换PY文件的方法
  • HttpServletRequest 和 HttpServletResponse核心接口区别
  • 哈希的概念及其应用
  • linux线程封装和互斥
  • Flutter Chen Generator - yaml配置使用
  • 了解SQL
  • 从姑苏区人工智能大模型基础设施招标|学习服务器、AI处理器、GPU
  • 【车联网kafka】Kafka核心架构与实战经验(第二篇)
  • 防火墙安全实验
  • 《秋招在即!Redis数据类型面试题解析》
  • Vue3+Vite项目如何简单使用tsx
  • SpringBoot+SpringAI打造智能对话机器人
  • MySQL 8.0 OCP 1Z0-908 题目解析(38)
  • Kafka Streams窗口技术全解析:从理论到电商实时分析实战
  • TTS语音合成|GPT-SoVITS语音合成服务器部署,实现http访问
  • Linux多线程线程控制
  • 前端核心技术Node.js(五)——Mongodb、Mongoose和接口
  • 计算机网络学习(一、Cisco Packet Tracer软件安装)
  • 计算机网络学习--------三次握手与四次挥手
  • diffusion原理和代码延伸笔记1——扩散桥,GOUB,UniDB
  • 【计算机网络】5传输层
  • 网络与信息安全有哪些岗位:(4)应急响应工程师
  • 【网络安全】等级保护2.0解决方案
  • 物联网与AI深度融合,赋能企业多样化物联需求
  • Redis实战(4)-- BitMap结构与使用
  • 基于单片机智能油烟机设计/厨房排烟系统设计
  • 用Python绘制SM2国密算法椭圆曲线:一场数学与视觉的盛宴
  • XML 用途
  • MVS相机+YOLO检测方法
  • 基于N32G45x+RTT驱动框架的定时器外部计数