Java学习教程,从入门到精通,Java 正则表达式知识点及案例代码(120)

Java 正则表达式知识点及案例代码

一、正则表达式简介

正则表达式(Regular Expression,简称 regex)是一种用于描述字符串模式的强大工具。它可以用来进行字符串的匹配、查找、替换等操作。Java 提供了 java.util.regex 包来支持正则表达式。

二、Java 正则表达式语法

1. 基本语法
元字符 描述
. 匹配除换行符以外的任意字符
\d 匹配数字,等价于 [0-9]
\D 匹配非数字,等价于 [^0-9]
\w 匹配字母、数字、下划线,等价于 [a-zA-Z0-9_]
\W 匹配非字母、数字、下划线,等价于 [^a-zA-Z0-9_]
\s 匹配空白字符,包括空格、制表符、换行符等
\S 匹配非空白字符
^ 匹配字符串的开头
$ 匹配字符串的结尾
\b 匹配单词边界
\B 匹配非单词边界
2. 字符类
表达式 描述
[abc] 匹配 abc
[^abc] 匹配除 abc 以外的任意字符
[a-z] 匹配 az 之间的任意小写字母
[A-Z] 匹配 AZ 之间的任意大写字母
[0-9] 匹配 09 之间的任意数字
3. 量词
量词 描述
* 匹配前面的子表达式零次或多次
+ 匹配前面的子表达式一次或多次
? 匹配前面的子表达式零次或一次
{n} 匹配前面的子表达式恰好 n
{n,} 匹配前面的子表达式至少 n
{n,m} 匹配前面的子表达式至少 n 次,至多 m
4. 分组和捕获
表达式 描述
(pattern) 匹配 pattern 并捕获匹配的子字符串
(?:pattern) 匹配 pattern 但不捕获匹配的子字符串
(?pattern) 匹配 pattern 并捕获匹配的子字符串,命名为 name
5. 边界匹配
表达式 描述
^ 匹配字符串的开头
$ 匹配字符串的结尾
\b 匹配单词边界
\B 匹配非单词边界
6. 预定义字符类
表达式 描述
\d 匹配数字,等价于 [0-9]
\D 匹配非数字,等价于 [^0-9]
\w 匹配字母、数字、下划线,等价于 [a-zA-Z0-9_]
\W 匹配非字母、数字、下划线,等价于 [^a-zA-Z0-9_]
\s 匹配空白字符,包括空格、制表符、换行符等
\S 匹配非空白字符
7. 转义字符
表达式 描述
\\ 匹配反斜杠 \
\. 匹配点号 .
\* 匹配星号 *
\+ 匹配加号 +
\? 匹配问号 ?
\( 匹配左括号 (
\) 匹配右括号 )
\{ 匹配左花括号 {
\} 匹配右花括号 }
\[ 匹配左方括号 [
\] 匹配右方括号 ]
| 匹配竖线 `

三、Java 正则表达式 API

Java 提供了 java.util.regex 包来支持正则表达式,主要包含以下类:

  • Pattern:表示一个正则表达式的编译表示。
  • Matcher:用于对字符串进行匹配操作的引擎。
1. Pattern
  • Pattern.compile(String regex):将给定的正则表达式编译为 Pattern 对象。
  • Pattern.matcher(CharSequence input):创建一个匹配器,用于匹配给定的输入字符串。
2. Matcher
  • matcher.matches():尝试将整个输入序列与模式匹配。
  • matcher.find():尝试查找与该模式匹配的输入序列的下一个子序列。
  • matcher.group():返回匹配的子字符串。
  • matcher.start():返回匹配的子字符串的起始索引。
  • matcher.end():返回匹配的子字符串的结束索引。

四、案例代码

import java.util.regex.*;

public class RegexExample {
    public static void main(String[] args) {
        // 1. 匹配数字
        String regex1 = "\\d+"; // 匹配一个或多个数字
        String input1 = "12345";
        System.out.println("1. 匹配数字: " + input1.matches(regex1)); // true

        // 2. 匹配邮箱
        String regex2 = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$";
        String input2 = "[email protected]";
        System.out.println("2. 匹配邮箱: " + input2.matches(regex2)); // true

        // 3. 匹配手机号码
        String regex3 = "^1[3456789]\\d{9}$";
        String input3 = "13800138000";
        System.out.println("3. 匹配手机号码: " + input3.matches(regex3)); // true

        // 4. 匹配日期格式 (YYYY-MM-DD)
        String regex4 = "^\\d{4}-\\d{2}-\\d{2}$";
        String input4 = "2023-10-01";
        System.out.println("4. 匹配日期格式: " + input4.matches(regex4)); // true

        // 5. 查找字符串中的所有数字
        String regex5 = "\\d+";
        String input5 = "abc123def456ghi";
        Pattern pattern5 = Pattern.compile(regex5);
        Matcher matcher5 = pattern5.matcher(input5);
        System.out.println("5. 查找字符串中的所有数字:");
        while (matcher5.find()) {
            System.out.println("找到数字: " + matcher5.group());
        }

        // 6. 替换字符串中的数字为 *
        String regex6 = "\\d";
        String input6 = "abc123def456ghi";
        String result6 = input6.replaceAll(regex6, "*");
        System.out.println("6. 替换字符串中的数字为 *: " + result6);

        // 7. 分组捕获
        String regex7 = "(\\d{3})-(\\d{4})";
        String input7 = "123-4567";
        Pattern pattern7 = Pattern.compile(regex7);
        Matcher matcher7 = pattern7.matcher(input7);
        if (matcher7.matches()) {
            System.out.println("7. 分组捕获:");
            System.out.println("第一组: " + matcher7.group(1)); // 123
            System.out.println("第二组: " + matcher7.group(2)); // 4567
        }

        // 8. 命名分组捕获
        String regex8 = "(?\\d{3})-(?\\d{4})";
        String input8 = "123-4567";
        Pattern pattern8 = Pattern.compile(regex8);
        Matcher matcher8 = pattern8.matcher(input8);
        if (matcher8.matches()) {
            System.out.println("8. 命名分组捕获:");
            System.out.println("area: " + matcher8.group("area")); // 123
            System.out.println("number: " + matcher8.group("number")); // 4567
        }
    }
}

五、代码解释

  1. 匹配数字:使用 \\d+ 匹配一个或多个数字。
  2. 匹配邮箱:使用正则表达式匹配常见的邮箱格式。
  3. 匹配手机号码:匹配中国大陆的手机号码。
  4. 匹配日期格式:匹配 YYYY-MM-DD 格式的日期。
  5. 查找字符串中的所有数字:使用 Matcher.find() 方法查找字符串中的所有数字。
  6. **替换字符串中的数字为 ***:使用 replaceAll() 方法将字符串中的数字替换为 *
  7. 分组捕获:使用 () 进行分组捕获,并通过 group(int) 方法获取捕获的内容。
  8. 命名分组捕获:使用 (?pattern) 进行命名分组捕获,并通过 group(String) 方法获取捕获的内容。

六、总结

正则表达式是处理字符串的强大工具,掌握它可以极大地提高字符串处理的效率。通过 Java 的 PatternMatcher 类,我们可以轻松地进行字符串的匹配、查找、替换等操作。希望这份文档和代码示例能够帮助你更好地理解和使用 Java 正则表达式。

你可能感兴趣的:(编程语言如门,Java,大数据,java,学习,正则表达式,jdbc,开发语言,数据库,java后端开发)