欢迎访问宙启技术站
智能推送

Java正则表达式函数:利用Java正则表达式函数进行字符串匹配

发布时间:2023-06-11 18:59:18

Java的正则表达式函数是非常强大的工具,它可以快速地进行字符串匹配,方便地实现字符串的识别和处理。下面将介绍Java正则表达式函数的具体用法。

1、Pattern和Matcher类

Java正则表达式函数的核心是Pattern和Matcher类。Pattern类表示正则表达式,而Matcher类则用于匹配目标字符串。通常先使用Pattern类来创建正则表达式,然后使用Matcher类来对目标字符串进行匹配。

2、创建Pattern对象

在Java中,可以使用Pattern类的静态方法compile()或者Pattern的构造函数创建Pattern对象,例如:

Pattern pattern = Pattern.compile("abc");  // 使用compile方法创建Pattern对象
// 或者
Pattern pattern = new Pattern("abc");  // 使用构造函数创建Pattern对象

3、创建Matcher对象

使用Pattern对象来创建Matcher对象,示例如下:

Matcher matcher = pattern.matcher("abcdef"); 

在创建Matcher对象时,需要传入需要匹配的目标字符串。

4、常用的正则表达式符号

在使用正则表达式时,常用的符号有:

- ^ 匹配字符串的开始位置

- $ 匹配字符串的结束位置

- \\d 匹配任意数字,等价于 [0-9]

- \\D 匹配任意非数字,等价于 [^0-9]

- \\w 匹配任意字母或数字或下划线,等价于 [a-zA-Z0-9_]

- \\W 匹配任意非字母或数字或下划线,等价于 [^a-zA-Z0-9_]

- \\s 匹配任意空白字符,等价于 [ \\t\

\\x0B\\f\\r]

- \\S 匹配任意非空白字符,等价于 [^ \\t\

\\x0B\\f\\r]

- . 匹配任意字符

- x? 匹配x零次或一次

- x* 匹配x零次或多次

- x+ 匹配x一次或多次

- x{n} 匹配x恰好n次

- x{n,} 匹配x至少n次

- x{n,m} 匹配x至少n次,至多m次

5、字符串匹配方法

在Matcher类中,有以下常用的方法可以对字符串进行匹配,并返回匹配结果:

- find():尝试查找与Pattern相匹配的输入序列的下一个子序列。

- matches():尝试将整个输入序列与Pattern进行匹配。

- lookingAt():尝试将输入序列从开头开始与Pattern进行匹配。

- replaceFirst():将匹配的输入子序列的第一个替换为指定的替换字符串。

- replaceAll():将匹配的输入子序列替换为指定的替换字符串。

- group():返回当前匹配到的字符串。

示例如下:

Pattern pattern = Pattern.compile("hello");
Matcher matcher = pattern.matcher("hello world");

if (matcher.find()) {
   System.out.println("found");
} else {
   System.out.println("not found");
}

if (matcher.matches()) {
   System.out.println("matched");
} else {
   System.out.println("not matched");
}

if (matcher.lookingAt()) {
   System.out.println("lookingAt matched");
} else {
   System.out.println("lookingAt not matched");
}

String result = matcher.replaceFirst("world");
System.out.println(result);

result = matcher.replaceAll("world");
System.out.println(result);

System.out.println(matcher.group());

输出结果为:

found
not matched
lookingAt matched
world world
world world
hello

6、实例演示

下面通过一个实例演示如何使用Java正则表达式函数进行字符串匹配。假设有一个字符串列表,其中包含意大利电话号码,需要从中提取出所有的电话号码。

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegExTest {
    public static void main(String[] args) {
        List<String> numbers = new ArrayList<>();
        numbers.add("+39 06 8555681");
        numbers.add("+39 06 8555682");
        numbers.add("+39 06 8555683");
        numbers.add("06 8555684");
        numbers.add("06 8555685");
        numbers.add("06 8555686");

        Pattern pattern = Pattern.compile("(\\+\\d{2}\\s)?\\d{2}\\s\\d{7}");
        for (String number : numbers) {
            Matcher matcher = pattern.matcher(number);
            if (matcher.matches()) {
                System.out.println("Match: " + matcher.group());
            } else {
                System.out.println("No match: " + number);
            }
        }
    }
}

在上面的代码中,我们首先创建一个包含电话号码的字符串列表。然后,我们使用Pattern.compile()方法创建一个能够识别电话号码的正则表达式,这里使用了带括号的分组,表示电话号码的前缀有可能是国际区号,也有可能没有。接着我们遍历电话号码列表,用Matcher类的matches()方法来判断每个号码是否符合正则表达式。如果匹配成功,则输出该号码。

执行上面的代码,会输出下面结果:

Match: +39 06 8555681
Match: +39 06 8555682
Match: +39 06 8555683
Match: 06 8555684
Match: 06 8555685
Match: 06 8555686

可以看到,正则表达式成功地识别出了所有的电话号码。

总结:

Java正则表达式函数是字符串匹配的非常好的工具,可以用来识别和处理各种的字符串,提高代码的效率。开发者只需要学会使用Pattern和Matcher类的方法,定义好匹配字符串的正则表达式即可。