杰瑞科技汇

Java正则如何实现多个匹配?

Java 正则表达式多个匹配

在 Java 中,使用正则表达式进行多个匹配可以通过以下几种方式实现:

使用 PatternMatcher

import java.util.regex.*;
public class RegexMultipleMatches {
    public static void main(String[] args) {
        String text = "The quick brown fox jumps over the lazy dog. The fox is quick.";
        String regex = "\\bfox\\b"; // 匹配单词 "fox"
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        // 查找所有匹配项
        while (matcher.find()) {
            System.out.println("找到匹配: " + matcher.group() + 
                             " 位置: " + matcher.start() + "-" + matcher.end());
        }
    }
}

使用 String.split() 方法

import java.util.Arrays;
public class RegexSplit {
    public static void main(String[] args) {
        String text = "apple,banana,orange,grape";
        String regex = ","; // 以逗号分割
        String[] fruits = text.split(regex);
        System.out.println(Arrays.toString(fruits));
    }
}

使用 String.replaceAll()String.replaceFirst()

public class RegexReplace {
    public static void main(String[] args) {
        String text = "The quick brown fox jumps over the lazy dog. The fox is quick.";
        // 替换所有匹配项
        String replacedAll = text.replaceAll("\\bfox\\b", "cat");
        System.out.println("替换所有: " + replacedAll);
        // 只替换第一个匹配项
        String replacedFirst = text.replaceFirst("\\bfox\\b", "cat");
        System.out.println("替换第一个: " + replacedFirst);
    }
}

使用 Pattern.matches() 检查整个字符串是否匹配

public class RegexMatches {
    public static void main(String[] args) {
        String text = "123-456-7890";
        String regex = "\\d{3}-\\d{3}-\\d{4}"; // 匹配电话号码格式
        boolean matches = Pattern.matches(regex, text);
        System.out.println("字符串是否匹配正则表达式: " + matches);
    }
}

获取所有匹配项并存储到列表中

import java.util.ArrayList;
import java.util.List;
import java.util.regex.*;
public class RegexFindAll {
    public static void main(String[] args) {
        String text = "Emails: user1@example.com, user2@test.org, admin@domain.net";
        String regex = "\\b\\w+@\\w+\\.\\w+\\b"; // 匹配电子邮件地址
        List<String> emails = new ArrayList<>();
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            emails.add(matcher.group());
        }
        System.out.println("找到的电子邮件地址: " + emails);
    }
}

使用 Matcher.lookingAt() 从字符串开头匹配

import java.util.regex.*;
public class RegexLookingAt {
    public static void main(String[] args) {
        String text = "123-456-7890 is a phone number";
        String regex = "\\d{3}-\\d{3}-\\d{4}";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        if (matcher.lookingAt()) {
            System.out.println("从开头匹配成功: " + matcher.group());
        } else {
            System.out.println("从开头匹配失败");
        }
    }
}

注意事项

  1. 正则表达式中的特殊字符需要使用 \\ 进行转义
  2. 使用 find() 方法可以查找所有匹配项,而 matches() 只检查整个字符串是否匹配
  3. 对于复杂的匹配需求,可以考虑使用正则表达式的捕获组 和反向引用
  4. 性能方面,对于大量文本处理,预编译正则表达式 (Pattern.compile()) 会更高效

方法可以根据不同的需求选择使用,实现 Java 中的正则表达式多个匹配功能。

分享:
扫描分享到社交APP
上一篇
下一篇