java.util.regex 包介绍
java.util.regex
是 Java 提供的正则表达式(Regular Expression)处理包,主要用于字符串的模式匹配、查找、替换等操作。它包含两个核心类:
Pattern
- 编译正则表达式Matcher
- 执行匹配操作
1. 核心类
(1) Pattern
(正则表达式编译)
- 用于编译正则表达式,生成
Pattern
对象。 - 线程安全,适合复用。
主要方法
方法 | 说明 |
---|---|
static Pattern compile(String regex) | 编译正则表达式 |
Matcher matcher(CharSequence input) | 创建 Matcher 对象 |
static boolean matches(String regex, CharSequence input) | 直接匹配(简化版) |
String pattern() | 返回正则表达式字符串 |
示例
Pattern pattern = Pattern.compile("a*b"); // 编译正则表达式
Matcher matcher = pattern.matcher("aaab"); // 创建 Matcher
boolean isMatch = matcher.matches(); // true
(2) Matcher
(匹配操作)
- 用于对字符串进行匹配、查找、替换等操作。
主要方法
方法 | 说明 |
---|---|
boolean matches() | 整个字符串是否匹配 |
boolean find() | 查找下一个匹配的子串 |
String group() | 返回匹配的子串 |
int start() , int end() | 返回匹配的起始/结束索引 |
String replaceAll(String replacement) | 替换所有匹配的子串 |
String replaceFirst(String replacement) | 替换第一个匹配的子串 |
示例
Pattern pattern = Pattern.compile("\\d+"); // 匹配数字
Matcher matcher = pattern.matcher("abc123xyz456");
while (matcher.find()) {
System.out.println("找到数字: " + matcher.group());
}
输出:
找到数字: 123
找到数字: 456
2. 常用正则表达式语法
表达式 | 说明 |
---|---|
\d | 数字 [0-9] |
\D | 非数字 [^0-9] |
\w | 单词字符 [a-zA-Z0-9_] |
\W | 非单词字符 |
\s | 空白字符(空格、制表符等) |
\S | 非空白字符 |
. | 任意字符(除换行符 \n ) |
* | 0 次或多次 |
+ | 1 次或多次 |
? | 0 次或 1 次 |
{n} | 恰好 n 次 |
{n,} | 至少 n 次 |
{n,m} | n 到 m 次 |
^ | 字符串开头 |
$ | 字符串结尾 |
[abc] | 匹配 a、b 或 c |
[^abc] | 不匹配 a、b、c |
`(a | b)` |
3. 常见应用场景
(1) 验证字符串格式
// 验证手机号(11位数字)
String phone = "13800138000";
boolean isValid = phone.matches("\\d{11}"); // true
// 验证邮箱
String email = "test@example.com";
boolean isEmail = email.matches("\\w+@\\w+\\.\\w+"); // true
(2) 查找字符串中的数字
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher("价格: 99元, 折扣: 20%");
while (matcher.find()) {
System.out.println("数字: " + matcher.group());
}
输出:
数字: 99
数字: 20
(3) 替换字符串
String text = "Java 8, Java 11, Java 17";
String updated = text.replaceAll("Java \\d+", "JDK"); // "JDK, JDK, JDK"
(4) 提取分组
// 提取日期(年、月、日)
String dateStr = "2023-10-25";
Pattern pattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
Matcher matcher = pattern.matcher(dateStr);
if (matcher.matches()) {
String year = matcher.group(1); // 2023
String month = matcher.group(2); // 10
String day = matcher.group(3); // 25
}
4. 注意事项
- 转义字符:在 Java 字符串中,
\
需要写成\\
,如\\d
表示\d
。 - 性能优化:
- 多次使用同一个正则表达式时,预编译
Pattern
以提高性能。 - 避免在循环中重复编译正则表达式。
- 多次使用同一个正则表达式时,预编译
- 贪婪匹配 vs 非贪婪匹配:
.*
(贪婪匹配,尽可能匹配更多字符).*?
(非贪婪匹配,尽可能匹配更少字符)
- 线程安全:
Pattern
是线程安全的,可以复用。Matcher
不是线程安全的,每个线程应该有自己的Matcher
。
5. 完整示例
import java.util.regex.*;
public class RegexExample {
public static void main(String[] args) {
// 1. 验证手机号
String phone = "13800138000";
boolean isPhoneValid = phone.matches("\\d{11}");
System.out.println("手机号是否有效: " + isPhoneValid); // true
// 2. 查找所有数字
Pattern numberPattern = Pattern.compile("\\d+");
Matcher numberMatcher = numberPattern.matcher("价格: 99元, 折扣: 20%");
while (numberMatcher.find()) {
System.out.println("找到数字: " + numberMatcher.group());
}
// 3. 替换字符串
String text = "Java 8, Java 11, Java 17";
String updatedText = text.replaceAll("Java \\d+", "JDK");
System.out.println(updatedText); // JDK, JDK, JDK
// 4. 提取分组(年-月-日)
String dateStr = "2023-10-25";
Pattern datePattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
Matcher dateMatcher = datePattern.matcher(dateStr);
if (dateMatcher.matches()) {
System.out.println("年: " + dateMatcher.group(1));
System.out.println("月: " + dateMatcher.group(2));
System.out.println("日: " + dateMatcher.group(3));
}
}
}
6. 总结
Pattern
:编译正则表达式,生成可复用的匹配模式。Matcher
:执行匹配、查找、替换等操作。- 常见用途:
- 数据验证(邮箱、手机号、日期)
- 字符串查找(提取数字、URL、关键词)
- 字符串替换(批量修改文本)
- 分组提取(解析结构化数据)
掌握 java.util.regex
可以让你更高效地处理字符串! 🚀