Java Matcher appendReplacement() 方法

2024 年 11 月 9 日 | 阅读 2 分钟

此方法用于实现追加和替换步骤。首先,它将编译的字符或单词替换为给定的输入序列,然后将给定的替换字符串追加到字符串缓冲区。

语法

参数

strbuff - 目标字符串缓冲区

replcmnt - 替换字符串

返回值

此匹配器

抛出

IllegalStateException - 如果尚未尝试匹配,或者如果之前的匹配操作失败

IllegalArgumentException - 如果替换字符串引用了一个模式中不存在的命名捕获组

IndexOutOfBoundsException - 如果替换字符串引用了一个模式中不存在的捕获组

示例 1

输出

'Let the cat
'Let the cat come out of the bag'.To disclose a secret(cat

示例 2

输出

'Let the cat come out of the bag'.To disclose a secret(cat)**

示例 3

输出

Start :  0 End : 3
Start :  8 End : 11
Start :  15 End : 19
-Hello-word-