06.正则定位符

正则定位符

在这里插入图片描述

public class RegExp06 {
    public static void main(String[] args){
        String content = "123abcweihong133hong";
        //String resStr = "^[0-9]+[a-z]*";    //以至少1个数字开头,后接任意个小写字母的字符串   123abc
        //String resStr = "^[0-9]+\\_[a-z]+$";   //以至少一个数字开头,必须以至少一个小写字母结束
        //String resStr = "^[0-9]+\\-[a-z]+$";    //以至少一个数字开头,以”-“连接外加以至少一个小写字母结束
        // \\b:匹配字符串边界  hong\\b 匹配的就是字符串最后的红
        // \\B:匹配字符串非边界的字符
        String resStr = "hong\\B";
        //1.构造模式对象Pattern
        Pattern pattern = Pattern.compile(resStr);
        //2.创造匹配器对象Matcher
        Matcher matcher =pattern.matcher(content);
        while (matcher.find()){
            System.out.println("找到:"+matcher.group(0));
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_41239465/article/details/121493766