目录

  1. 正则表达式概述
  2. Java 中的正则表达式 API
  3. 常见正则表达式语法
  4. Java 正则表达式匹配示例
  5. Java 正则表达式替换
  6. Java 正则表达式分割字符串
  7. 高级应用:查找和提取子字符串
  8. 参考资料
  9. 出站链接

1. 正则表达式概述

正则表达式(Regular Expression, regex)是一种用于匹配字符串模式的强大工具。Java 提供了 java.util.regex 包来支持正则表达式操作。

常见应用:

  • 电子邮件验证
  • URL 解析
  • 日志文件分析
  • 字符串查找与替换

2. Java 中的正则表达式 API

Java 提供了 java.util.regex 包,其中主要包含三个类:

Pattern

Pattern 用于定义正则表达式,它是一个不可变对象,通常与 Matcher 结合使用。

示例:

import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("\\d+"); // 匹配数字
        System.out.println("Pattern: " + pattern.pattern());
    }
}

Matcher

Matcher 用于执行匹配操作,如查找、替换等。

示例:

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("\\d+"); // 匹配数字
        Matcher matcher = pattern.matcher("今天是 2025 年 3 月 23 日");

        while (matcher.find()) {
            System.out.println("找到的数字: " + matcher.group());
        }
    }
}

PatternSyntaxException 异常

当正则表达式语法错误时,会抛出 PatternSyntaxException

示例:

import java.util.regex.PatternSyntaxException;

public class Main {
    public static void main(String[] args) {
        try {
            Pattern.compile("[a-z"); // 错误的正则表达式
        } catch (PatternSyntaxException e) {
            System.out.println("正则表达式错误: " + e.getDescription());
        }
    }
}


3. 常见正则表达式语法

表达式说明
.匹配任意字符(换行符除外)
\d匹配数字 [0-9]
\D匹配非数字
\w匹配字母、数字、下划线 [a-zA-Z0-9_]
\W匹配非字母、数字、下划线
\s匹配空格(空格、Tab、换行)
\S匹配非空格
^匹配字符串的开头
$匹配字符串的结尾
*匹配 0 次或多次
+匹配 1 次或多次
?匹配 0 次或 1 次
{n}匹配 n 次
{n,}至少匹配 n 次
{n,m}匹配 n 到 m 次
[abc]匹配 a、b 或 c
[^abc]匹配非 a、b、c 的字符
`(ab

4. Java 正则表达式匹配示例

示例:验证电子邮件

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        String email = "example@test.com";
        String regex = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$";
        
        boolean isMatch = Pattern.matches(regex, email);
        System.out.println("邮箱格式是否正确: " + isMatch);
    }
}


5. Java 正则表达式替换

示例:替换所有数字

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        String text = "今天是 2025 年 3 月 23 日";
        String newText = text.replaceAll("\\d", "*");
        System.out.println("替换后: " + newText);
    }
}


6. Java 正则表达式分割字符串

示例:按逗号分割

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        String text = "苹果,香蕉,葡萄,橘子";
        String[] fruits = text.split(",");
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
    }
}


7. 高级应用:查找和提取子字符串

示例:提取所有电话号码

import java.util.regex.*;

public class Main {
    public static void main(String[] args) {
        String text = "联系我: 137-8888-9999 或 188-5555-6666";
        String regex = "\\d{3}-\\d{4}-\\d{4}";
        
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        
        while (matcher.find()) {
            System.out.println("找到的电话号码: " + matcher.group());
        }
    }
}


参考资料


出站链接