java正则表达式用法

String str = "     public static final String FAILED_KEY = \"faild\";             // 获取key,  错误";
        String regx = "\\s*public static final String (\\S+) = \"(\\S+)\";\\s*//\\s*(\\S+(\\s*\\S+)*)";
//        String str = "     public static final String SUCCESS = \"0\"; // 0";
//        Pattern p = Pattern.compile("public static final String (\\S+) = \"(\\S+)\";  //(\\S+)");
        Pattern p = Pattern.compile(regx);
        Matcher m = p.matcher(str);
        // matcher的matcher方法,能够匹配到返回true
        if(m.matches()) {
            System.out.println("group:"+m.group());// 输出本身
            System.out.println("group[0]:"+m.group(0));// 输出本身
            System.out.println("group[1]:"+m.group(1));// 输出括号中第一个子项
            System.out.println("group[2]:"+m.group(2));// 输出口号中第二个子项
            System.out.println("group[3]:"+m.group(3));// 输出括号中第三个子项
            // (\s*\S+)* ()会作为一个整体,表示括号中的内容不一定能够执行,有就进行匹配,没有就不匹配
        }

 

posted @   初见洞洞拐  阅读(56)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示