09.非贪婪匹配

非贪婪匹配

String resStr = “\d+?”; //非贪婪匹配

public class RegExp09 {
    public static void main(String[] args){
        String content = "111hello123";
        //String resStr = "\\d+"; //默认是贪婪匹配
        String resStr = "\\d+?"; //非贪婪匹配
        //1.构造模式对象Pattern
        Pattern pattern = Pattern.compile(resStr);
        //2.创造匹配器对象Matcher
        Matcher matcher =pattern.matcher(content);
        while (matcher.find()){
            System.out.println("找到:"+matcher.group(0));
        }

    }
}

程序结果:

在这里插入图片描述

Guess you like

Origin blog.csdn.net/qq_41239465/article/details/121493969