Java

Java Matcher如何设置匹配模式为不区分大小写

小樊
81
2024-08-17 15:58:31
栏目: 编程语言

要设置Java Matcher的匹配模式为不区分大小写,可以使用Pattern.CASE_INSENSITIVE标志。示例如下:

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

public class Main {
    public static void main(String[] args) {
        String text = "Hello, World!";
        Pattern pattern = Pattern.compile("hello", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(text);

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

在上面的示例中,使用Pattern.compile方法并传递Pattern.CASE_INSENSITIVE标志来创建不区分大小写的匹配模式。然后使用Matcher对象来查找匹配项。

0
看了该问题的人还看了