Java Matcher 类
Java 中的 Matcher
类是 java.util.regex
包中的一个重要类,它用于对字符串执行各种匹配操作。Matcher
类本身不能直接实例化,而是需要通过 Pattern
类的 matcher()
方法来创建。
Matcher
类的主要功能包括:
- 执行正则表达式的匹配操作
- 查找字符串中与模式匹配的子序列
- 替换匹配到的文本
创建 Matcher 对象
要使用 Matcher
类,首先需要创建一个 Pattern
对象,然后通过它来创建 Matcher
对象:
实例
import java.util.regex.*;
// 创建 Pattern 对象
Pattern pattern = Pattern.compile("a*b");
// 创建 Matcher 对象
Matcher matcher = pattern.matcher("aaaaab");
// 创建 Pattern 对象
Pattern pattern = Pattern.compile("a*b");
// 创建 Matcher 对象
Matcher matcher = pattern.matcher("aaaaab");
Matcher 类的常用方法
匹配操作方法
matches() 方法
matches()
方法尝试将整个输入序列与模式进行匹配:
实例
boolean result = matcher.matches(); // 返回 true 或 false
lookingAt() 方法
lookingAt()
方法尝试从输入序列的开头开始匹配模式:
实例
boolean result = matcher.lookingAt(); // 不需要匹配整个字符串
find() 方法
find()
方法在输入序列中查找下一个匹配的子序列:
实例
while(matcher.find()) {
System.out.println("找到匹配: " + matcher.group());
}
System.out.println("找到匹配: " + matcher.group());
}
获取匹配信息的方法
group() 方法
group()
方法返回前一次匹配操作所匹配的输入子序列:
实例
String matchedText = matcher.group();
start() 和 end() 方法
start()
和 end()
方法返回前一次匹配的开始和结束索引:
实例
int start = matcher.start(); // 匹配开始位置
int end = matcher.end(); // 匹配结束位置
int end = matcher.end(); // 匹配结束位置
groupCount() 方法
groupCount()
返回此匹配器模式中的捕获组数:
实例
int count = matcher.groupCount();
替换操作方法
replaceAll() 方法
replaceAll()
方法替换输入序列中与模式匹配的每个子序列:
实例
String result = matcher.replaceAll("replacement");
replaceFirst() 方法
replaceFirst()
方法替换输入序列中与模式匹配的第一个子序列:
实例
String result = matcher.replaceFirst("replacement");
实例
下面是一个完整的示例,展示如何使用 Matcher
类:
实例
import java.util.regex.*;
public class MatcherExample {
public static void main(String[] args) {
String text = "The quick brown fox jumps over the lazy dog";
Pattern pattern = Pattern.compile("\\b\\w{4}\\b"); // 匹配4字母单词
Matcher matcher = pattern.matcher(text);
System.out.println("原始文本: " + text);
System.out.println("匹配4字母单词:");
while(matcher.find()) {
System.out.println("找到 '" + matcher.group() +
"' 在位置 " + matcher.start() + "-" + (matcher.end()-1));
}
// 替换所有4字母单词为"****"
String replaced = matcher.replaceAll("****");
System.out.println("\n替换后的文本: " + replaced);
}
}
public class MatcherExample {
public static void main(String[] args) {
String text = "The quick brown fox jumps over the lazy dog";
Pattern pattern = Pattern.compile("\\b\\w{4}\\b"); // 匹配4字母单词
Matcher matcher = pattern.matcher(text);
System.out.println("原始文本: " + text);
System.out.println("匹配4字母单词:");
while(matcher.find()) {
System.out.println("找到 '" + matcher.group() +
"' 在位置 " + matcher.start() + "-" + (matcher.end()-1));
}
// 替换所有4字母单词为"****"
String replaced = matcher.replaceAll("****");
System.out.println("\n替换后的文本: " + replaced);
}
}
输出结果
原始文本: The quick brown fox jumps over the lazy dog 匹配4字母单词: 找到 'quick' 在位置 4-8 找到 'brown' 在位置 10-14 找到 'jumps' 在位置 20-24 找到 'over' 在位置 26-29 找到 'lazy' 在位置 35-38 替换后的文本: The **** **** fox **** **** the **** dog
总结
Java 的 Matcher
类提供了强大的正则表达式匹配功能,通过它可以:
- 检查字符串是否匹配特定模式
- 查找字符串中的模式匹配
- 提取匹配的子字符串
- 替换匹配的文本
掌握 Matcher
类对于处理字符串和文本模式匹配非常重要,是 Java 编程中一个非常实用的工具。
匹配操作方法
方法 | 描述 |
---|---|
boolean matches() |
尝试将整个区域与模式匹配 |
boolean lookingAt() |
尝试从区域开头开始匹配模式 |
boolean find() |
尝试查找下一个匹配的子序列 |
boolean find(int start) |
从指定位置开始查找下一个匹配 |
匹配结果获取方法
方法 | 描述 |
---|---|
String group() |
返回前一次匹配的子序列 |
String group(int group) |
返回指定捕获组的匹配内容 |
String group(String name) |
返回命名捕获组的匹配内容(Java 7+) |
int groupCount() |
返回模式中的捕获组数量 |
int start() |
返回前一次匹配的起始索引 |
int start(int group) |
返回指定组的匹配起始索引 |
int end() |
返回前一次匹配的结束索引 |
int end(int group) |
返回指定组的匹配结束索引 |
替换操作方法
方法 | 描述 |
---|---|
Matcher appendReplacement(StringBuffer sb, String replacement) |
执行非终端替换操作 |
StringBuffer appendTail(StringBuffer sb) |
执行终端替换操作 |
String replaceAll(String replacement) |
替换所有匹配的子序列 |
String replaceFirst(String replacement) |
替换第一个匹配的子序列 |
static String quoteReplacement(String s) |
返回替换字符串的字面量 |
区域控制方法
方法 | 描述 |
---|---|
Matcher region(int start, int end) |
设置匹配器的搜索区域 |
int regionStart() |
返回匹配器区域的起始索引 |
int regionEnd() |
返回匹配器区域的结束索引 |
boolean hasTransparentBounds() |
检查是否使用透明边界 |
Matcher useTransparentBounds(boolean b) |
设置是否使用透明边界 |
boolean hasAnchoringBounds() |
检查是否使用锚定边界 |
Matcher useAnchoringBounds(boolean b) |
设置是否使用锚定边界 |
其他方法
方法 | 描述 |
---|---|
Pattern pattern() |
返回此匹配器的模式 |
Matcher reset() |
重置匹配器 |
Matcher reset(CharSequence input) |
用新输入重置匹配器 |
boolean hitEnd() |
指示匹配器是否到达输入结尾 |
boolean requireEnd() |
指示是否需要更多输入才能匹配 |
点我分享笔记