在 Java 中,Matcher 类是用于匹配正则表达式的工具,而 group() 方法是 Matcher 类中的一个重要方法,用于提取匹配结果中的捕获组(captured groups)。以下是对 group() 方法的详细解释:
在正则表达式中,捕获组是通过圆括号 () 定义的。每个 () 内的子表达式都是一个捕获组,匹配的内容可以被单独提取。
例如:
1 |
String regex = "(\\d{2})-(\\d{2})-(\\d{4})"; |
这个正则表达式匹配日期格式(如 12-31-2023),其中:
(\\d{2}) 是第一个捕获组,匹配月份。
(\\d{2}) 是第二个捕获组,匹配日期。
(\\d{4}) 是第三个捕获组,匹配年份。
Matcher 类用于对输入字符串进行正则表达式匹配。它通过 Pattern 类的 matcher() 方法创建。
示例:
1 2 |
Pattern pattern = Pattern.compile("(\\d{2})-(\\d{2})-(\\d{4})"); Matcher matcher = pattern.matcher("12-31-2023"); |
group() 方法用于提取匹配结果中的捕获组。它有几种重载形式:
返回整个匹配的字符串。
如果没有匹配成功,调用此方法会抛出 IllegalStateException。
示例:
1 2 3 |
if (matcher.find()) { System.out.println(matcher.group()); // 输出 "12-31-2023" } |
返回指定捕获组的匹配内容。
捕获组的编号从 1 开始,group(0) 等价于 group(),表示整个匹配的字符串。
如果指定的捕获组不存在,会抛出 IndexOutOfBoundsException。
示例:
1 2 3 4 5 |
if (matcher.find()) { System.out.println(matcher.group(1)); // 输出 "12"(月份) System.out.println(matcher.group(2)); // 输出 "31"(日期) System.out.println(matcher.group(3)); // 输出 "2023"(年份) } |
返回命名捕获组的匹配内容。
命名捕获组通过 (?<name>...) 语法定义。
如果指定的命名捕获组不存在,会抛出 IllegalArgumentException。
示例:
1 2 3 4 5 6 7 8 |
Pattern pattern = Pattern.compile("(?<month>\\d{2})-(?<day>\\d{2})-(?<year>\\d{4})"); Matcher matcher = pattern.matcher("12-31-2023");
if (matcher.find()) { System.out.println(matcher.group("month")); // 输出 "12" System.out.println(matcher.group("day")); // 输出 "31" System.out.println(matcher.group("year")); // 输出 "2023" } |
编译正则表达式:
1 |
Pattern pattern = Pattern.compile("(\\d{2})-(\\d{2})-(\\d{4})"); |
创建 Matcher 对象:
1 |
Matcher matcher = pattern.matcher("12-31-2023"); |
执行匹配:
使用 find() 方法查找匹配项。
或者使用 matches() 方法检查整个字符串是否匹配。
提取捕获组:
使用 group() 方法提取匹配的内容。
示例:
1 2 3 4 5 6 |
if (matcher.find()) { String month = matcher.group(1); // "12" String day = matcher.group(2); // "31" String year = matcher.group(3); // "2023" System.out.println("Month: " + month + ", Day: " + day + ", Year: " + year); } |
匹配成功后才能调用 group():
在调用 group() 之前,必须先调用 find() 或 matches() 方法,否则会抛出 IllegalStateException。
捕获组编号从 1 开始:
group(0) 表示整个匹配的字符串,group(1) 表示第一个捕获组,依此类推。
捕获组不存在时抛出异常:
如果指定的捕获组编号或名称不存在,会抛出 IndexOutOfBoundsException 或 IllegalArgumentException。
命名捕获组需要 Java 7+:
命名捕获组功能在 Java 7 及以上版本中支持。
以下是一个完整的示例,演示如何使用 group() 方法提取捕获组:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.regex.Matcher; import java.util.regex.Pattern;
public class RegexExample { public static void main(String[] args) { String input = "Date: 12-31-2023, Time: 23:59"; String regex = "(\\d{2})-(\\d{2})-(\\d{4}).*?(\\d{2}):(\\d{2})"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input);
if (matcher.find()) { System.out.println("Month: " + matcher.group(1)); // "12" System.out.println("Day: " + matcher.group(2)); // "31" System.out.println("Year: " + matcher.group(3)); // "2023" System.out.println("Hour: " + matcher.group(4)); // "23" System.out.println("Minute: " + matcher.group(5));// "59" } else { System.out.println("No match found!"); } } } |
group() 方法是 Matcher 类的核心方法,用于提取正则表达式匹配的捕获组。
捕获组通过圆括号 () 定义,编号从 1 开始。
使用 group() 前必须调用 find() 或 matches() 方法。
命名捕获组(Java 7+)可以通过名称提取匹配内容。
作用:
检查整个输入字符串是否完全匹配正则表达式。
如果整个字符串与正则表达式匹配,返回 true;否则返回 false。
匹配范围:
必须从字符串的开头匹配到结尾。
示例:
1 2 3 4 5 6 7 8 9 10 |
String regex = "a.b"; // 匹配 "a" + 任意字符 + "b" String input1 = "aab"; String input2 = "aabb";
Pattern pattern = Pattern.compile(regex); Matcher matcher1 = pattern.matcher(input1); Matcher matcher2 = pattern.matcher(input2);
System.out.println(matcher1.matches()); // true,因为 "aab" 完全匹配 "a.b" System.out.println(matcher2.matches()); // false,因为 "aabb" 不完全匹配 "a.b" |
适用场景:
当需要检查整个字符串是否符合某种格式时(例如验证邮箱、电话号码等)。
作用:
在输入字符串中查找与正则表达式匹配的子串。
如果找到匹配的子串,返回 true;否则返回 false。
可以多次调用,每次调用会查找下一个匹配的子串。
匹配范围:
不要求整个字符串匹配,只要字符串中包含与正则表达式匹配的子串即可。
示例:
1 2 3 4 5 6 7 8 9 |
String regex = "a.b"; // 匹配 "a" + 任意字符 + "b" String input = "aab aabb";
Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input);
while (matcher.find()) { System.out.println("Found: " + matcher.group()); // 输出匹配的子串 } |
输出:
复制
1 2 |
Found: aab Found: aab |
适用场景:
当需要从字符串中提取多个匹配的子串时(例如从日志中提取特定格式的数据)。
特性 | matches() | find() |
---|---|---|
匹配范围 | 整个字符串必须完全匹配正则表达式。 | 字符串中只要包含匹配的子串即可。 |
返回值 | true 或 false。 | true 或 false。 |
多次调用 | 每次调用都检查整个字符串。 | 每次调用查找下一个匹配的子串。 |
适用场景 | 验证字符串是否符合某种格式。 | 提取字符串中符合某种模式的子串。 |