Matcher group方法

在Java中,`Matcher` 类的 `group` 方法用于捕获由正则表达式匹配的文本。以下是 `group` 方法的一些常见用法和说明:

### 基本语法:

```java
public String group()
public String group(int group)
```

- `group()`:返回匹配整个正则表达式的输入字符串。
- `group(int group)`:返回由正则表达式中的捕获组匹配的输入字符串。组号从1开始,0 表示整个表达式。

### 返回值:

- 返回 `String` 类型,即匹配的文本或捕获组中的文本。

### 示例用法:

假设我们有以下正则表达式和输入字符串:

```java
String regex = "(\\w+)@(\\w+)\\.com";
String input = "test@example.com";
```

我们可以使用 `Pattern` 和 `Matcher` 来匹配输入字符串,并使用 `group` 方法获取捕获组:

```java
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MatcherGroupExample {
    public static void main(String[] args) {
        String regex = "(\\w+)@(\\w+)\\.com";
        String input = "test@example.com";

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        if (matcher.find()) {
            // 获取整个匹配的字符串
            String fullMatch = matcher.group();
            System.out.println("Full match: " + fullMatch);

            // 获取第一个捕获组(用户名)
            String username = matcher.group(1);
            System.out.println("Username: " + username);

            // 获取第二个捕获组(域名)
            String domain = matcher.group(2);
            System.out.println("Domain: " + domain);
        }
    }
}
```

在这个示例中:

- `matcher.find()` 用于查找输入字符串中与正则表达式匹配的部分。
- `matcher.group()` 返回整个匹配的字符串,即 "test@example.com"。
- `matcher.group(1)` 返回第一个捕获组匹配的字符串,即 "test"。
- `matcher.group(2)` 返回第二个捕获组匹配的字符串,即 "example"。

### 注意事项:

- 如果没有找到匹配项,`group` 方法将返回 `null`。
- 捕获组的索引从1开始,而不是0。组0始终表示整个正则表达式的匹配结果。
- 如果正则表达式中没有定义捕获组,或者捕获组没有匹配任何内容,`group(int group)` 将返回 `""`(空字符串)。

使用 `group` 方法可以方便地从匹配结果中提取所需的信息。
 

public class Test2 {
    public static void main(String[] args) {
        String htmlText = "<a href=\\\"www.baidu.com\\\">百度一下</a><hr><br>" +
                "<em>good</em><hr><br>" +
                "<div class=\\\"red\\\">奥利给</div>" +
                "<li>li标签</li>";
        //正则表达式字符串
        //匹配所有便签对中的文本
        String allText  = ">([^<]+)<";

        Pattern compile = Pattern.compile(allText);
        Matcher matcher = compile.matcher(htmlText);
        while (matcher.find()) {
            String textContent = matcher.group(1); // 获取捕获组1的内容
            System.out.println(textContent);
        }



    }
}

相关推荐

  1. 方 法

    2024-07-17 15:24:03       27 阅读
  2. \__new__()方法

    2024-07-17 15:24:03       47 阅读
  3. QList 方法

    2024-07-17 15:24:03       51 阅读
  4. charCodeAt() 方法

    2024-07-17 15:24:03       60 阅读
  5. ExecuteScalar()方法

    2024-07-17 15:24:03       56 阅读
  6. performClick()方法

    2024-07-17 15:24:03       51 阅读
  7. Go <span style='color:red;'>方法</span>

    Go 方法

    2024-07-17 15:24:03      44 阅读
  8. Go 方法

    2024-07-17 15:24:03       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-17 15:24:03       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-17 15:24:03       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-17 15:24:03       58 阅读
  4. Python语言-面向对象

    2024-07-17 15:24:03       69 阅读

热门阅读

  1. leetcode热题100.乘积最大子数组(动态规划进阶)

    2024-07-17 15:24:03       23 阅读
  2. 二叉树---二叉树的最大深度

    2024-07-17 15:24:03       20 阅读
  3. AI技术在企业招聘中的应用案例分析

    2024-07-17 15:24:03       25 阅读
  4. 土土土土土土土土圭

    2024-07-17 15:24:03       22 阅读
  5. ElasticSearch学习之路

    2024-07-17 15:24:03       22 阅读
  6. android include 和 merge 区别

    2024-07-17 15:24:03       20 阅读
  7. python基础篇(12):继承

    2024-07-17 15:24:03       23 阅读
  8. Spring解决循环依赖问题的四种方法

    2024-07-17 15:24:03       19 阅读
  9. 人工智能与人类社会的共生共荣

    2024-07-17 15:24:03       19 阅读