正则表达式是Java编程中处理字符串的强大工具,它能够帮助我们快速实现字符串的匹配、查找、替换和解析等操作。在本文中,我们将通过10个实战案例来解析Java正则表达式的使用,帮助读者轻松突破编程瓶颈。

1. 匹配简单字符串

案例描述:匹配任意三个字符的字符串。

正则表达式...

代码示例

String input = "abc def ghi";
Pattern pattern = Pattern.compile("\\.");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.println("找到匹配:" + matcher.group());
}

输出:找到匹配:abc

2. 匹配特定字符

案例描述:匹配包含数字的字符串。

正则表达式\d+

代码示例

String input = "abc 123 def 456";
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.println("找到匹配:" + matcher.group());
}

输出:找到匹配:123

3. 匹配特定字符范围

案例描述:匹配任意小写字母。

正则表达式[a-z]+

代码示例

String input = "Hello World!";
Pattern pattern = Pattern.compile("[a-z]+");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.println("找到匹配:" + matcher.group());
}

输出:找到匹配:ello

4. 匹配特定字符集

案例描述:匹配邮箱地址。

正则表达式[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}

代码示例

String input = "user@example.com";
Pattern pattern = Pattern.compile("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}");
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
    System.out.println("邮箱地址有效:" + input);
} else {
    System.out.println("邮箱地址无效:" + input);
}

输出:邮箱地址有效:user@example.com

5. 匹配重复的字符

案例描述:匹配连续重复的字符。

正则表达式([a-zA-Z])\\1+

代码示例

String input = "aaabbbccc";
Pattern pattern = Pattern.compile("([a-zA-Z])\\1+");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.println("找到匹配:" + matcher.group());
}

输出:找到匹配:aaabbbccc

6. 匹配边界

案例描述:匹配字符串的开始或结束。

正则表达式^...$...

代码示例

String input = "Hello World!";
Pattern pattern = Pattern.compile("^Hello");
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
    System.out.println("字符串以Hello开始");
} else {
    System.out.println("字符串不以Hello开始");
}

输出:字符串以Hello开始

7. 替换字符串

案例描述:将字符串中的特定部分替换为其他内容。

正则表达式...

代码示例

String input = "Hello World!";
Pattern pattern = Pattern.compile("World");
Matcher matcher = pattern.matcher(input);
String replaced = matcher.replaceAll("Java");
System.out.println(replaced);

输出:Hello Java

8. 分组

案例描述:匹配并提取字符串中的特定部分。

正则表达式(...)|(...)

代码示例

String input = "123-456-70";
Pattern pattern = Pattern.compile("(\\d{3})-(\\d{3})-(\\d{4})");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.println("区号:" + matcher.group(1) + ",电话号码:" + matcher.group(2) + "-" + matcher.group(3));
}

输出:区号:123,电话号码:456-70

9. 非贪婪模式

案例描述:匹配尽可能少的字符。

正则表达式.*?

代码示例

String input = "Hello World!";
Pattern pattern = Pattern.compile("H.*");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.println("找到匹配:" + matcher.group());
}

输出:找到匹配:H

10. 贪婪模式

案例描述:匹配尽可能多的字符。

正则表达式.*

代码示例

String input = "Hello World!";
Pattern pattern = Pattern.compile("H.*");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
    System.out.println("找到匹配:" + matcher.group());
}

输出:找到匹配:Hello World!

通过以上10个实战案例,相信读者已经对Java正则表达式的使用有了更深入的了解。在实际编程中,灵活运用正则表达式能够大大提高开发效率,解决各种字符串处理问题。