正则表达式find与matches区别详解

以下是您提供的 Java 程序清单的执行结果及核心要点解析。

正则表达式示例 (Listing 1-9)

程序清单 核心功能 正则表达式 输入字符串 输出结果 关键方法/行为
Listing 1 全字符串匹配 Java Java Matches mat.matches()要求整个输入字符串与模式完全匹配。
Java Java 9 No Match matches() 方法检查整个字符串,Java 9 包含空格,不匹配 Java。
Listing 2 查找子序列 Java Java 9 subsequence found mat.find() 在输入序列中查找下一个匹配的子序列。
Listing 3 查找多个子序列 test test 1 2 3 test test found at index 0 test found at index 11 while(mat.find()) 循环查找所有匹配项,mat.start() 返回匹配起始索引。
Listing 4 使用量词 + W+ W WW WWW Match: W Match: WW Match: WWW +表示匹配前一个字符一次或多次,mat.group() 返回匹配到的完整字符串。
Listing 5 贪婪匹配 e.+d extend cup end table Match: extend cup end . 匹配任意字符,+ 是贪婪量词,会匹配尽可能长的字符串。
Listing 6 勉强匹配 e.+?d extend cup end table Match: extend Match: end +? 是勉强量词,会匹配尽可能短的字符串。
Listing 7 字符类 [a-z]+ this is a test. Match: this Match: is Match: a Match: test [a-z]+ 匹配一个或多个小写字母,遇到非字母字符(空格、句点)则停止。
Listing 8 替换所有匹配项 Jon.*? Jon Jonathan Frank Ken Todd Original sequence: Jon Jonathan Frank Ken Todd Modified sequence: Eric Eric Frank Ken Todd mat.replaceAll("Eric ") 将所有匹配 Jon 后跟任意字符(勉强模式)直到空格的子串替换为 Eric 。
Listing 9 分割字符串 [ ,.!] one two,alpha9 12!done. Next token: one Next token: two Next token: alpha9 Next token: 12 Next token: done Next token: pat.split() 使用正则表达式作为分隔符进行分割,末尾的空字符串也会被输出。

反射示例 (Listing 10-11)

Listing 10 通过反射获取 java.awt.Dimension 类的构造方法、字段和公共方法信息。其输出类似于:

java 复制代码
Constructors:
 public java.awt.Dimension()
 public java.awt.Dimension(java.awt.Dimension)
 public java.awt.Dimension(int,int)
Fields:
 public int width
 public int height
Methods:
(会列出大量从 Object 和 Component 等父类继承的公共方法,如 `wait`, `notify`, `getClass`, `toString` 等)

Listing 11 演示如何获取并筛选一个自定义类 A 的公共方法。其输出为:

java 复制代码
Public Methods:
 a1
 a2

代码通过 getDeclaredMethods() 获取类声明的所有方法,再使用 Modifier.isPublic() 判断修饰符是否为 public。

RMI 示例 (Listing 12-15)

这是一个简单的远程方法调用 (RMI) 示例,由客户端调用服务器端的加法服务。

  1. Listing 12 (接口): 定义了远程接口 AddServerIntf,声明了 add 方法。

  2. Listing 13 (实现): AddServerImpl 类实现了远程接口,提供了 add 方法的具体逻辑。

  3. Listing 14 (服务器): AddServer 类创建远程对象实例并将其绑定到 RMI 注册表(名称为 "AddServer")。

  4. Listing 15 (客户端): AddClient 类通过 RMI 注册表查找远程对象,并调用其 add 方法。执行客户端时需要传递服务器主机名和两个加数作为参数 ,例如:

    复制代码
     

    输出将类似于:

    复制代码
    The first number is: 10.5
    The second number is: 20.3 The sum is: 30.8 ```

日期时间格式化示例 (Listing 16-22)

程序清单 使用的 API 核心功能 示例输出 (可能因运行环境而异)
Listing 16 DateFormat 不同地区、不同风格的日期格式化。 Japan: 2017/06/21 Korea: 2017. 6. 21. United Kingdom: 21 June 2017 United States: Wednesday, June 21, 2017
Listing 17 DateFormat 不同地区、不同风格的时间格式化。 Japan: 13:05 United Kingdom: 13:05:09 BST Canada: 1:05:09 p.m. BST
Listing 18 SimpleDateFormat 使用自定义模式格式化日期时间。 `
01:05:09<br>
21 Jun 2017 01:05:09 BST<br>Wed Jun 21 2017`
Listing 19 java.time (LocalDate/LocalTime) 获取当前日期和时间。 `
2017-06-21<br>
13:05:09.123`
Listing 20 java.time.format.DateTimeFormatter 使用预定义的本地化格式。 Wednesday, 21 June 2017 `
13:05`
Listing 21 java.time.format.DateTimeFormatter 使用自定义模式格式化日期时间。 June 21, 2017 1:05 PM
Listing 22 java.time.format.DateTimeFormatter 将字符串解析为日期时间对象,再格式化输出。 June 21, 2017 12:01 AM

参考来源

相关推荐
希和202710 小时前
江科大 51 单片机笔记:DS18B20 温度传感器 + LCD1602 显示代码详解
笔记·单片机·mongodb
Sakura_j_h_13 小时前
MCP SDK 2.0 实战:用 MCPServer 搭本地笔记检索服务
开发语言·笔记·python·qt·ai工具·mcp·mcpserver
前进的李工17 小时前
Agent 学习笔记 15:Agent 怎样响应事件与中途打断?
人工智能·笔记·agent·openclaw·工具设计·gpt-6 astra·原生异步
希和202720 小时前
51 单片机学习笔记:LCD1602、独立按键、I2C 与 AT24C02 掉电存储(江科大课程小白笔记)
笔记·单片机·学习
北风toto20 小时前
Spring Boot 常用注解 · 概要笔记
spring boot·笔记·后端
下一站春风里21 小时前
Spring IoC 控制反转 完整技术笔记
java·spring boot·笔记·spring
一只大鸽子1 天前
游戏开发笔记4-像素画基础 轮廓-直线和曲线
笔记
小杨小杨、努力变强!1 天前
python学习笔记
笔记·学习
杨逢昌工厂6S管理2 天前
150-杨逢昌一线现场诊断笔记:越管一线越抵触背后的熵增机理与基准化治理
经验分享·笔记·职场和发展·学习方法
Star(up up up)2 天前
Redis Windows 安装教程:使用Windows 的 Redis 压缩包
redis·笔记·学习·程序人生·云原生·学习方法·改行学it