正则表达式之以空格分割字符串但忽略引号中的内容


目标串: "aaa bbb "cc dd" eee fff"
期望结果:

aaa
bbb
"cc dd"
eee
fff

具体实现:

 

    private static final Pattern PATTERN = Pattern.compile("\"([^\"]*?)\"|(\\S+)");

    public static void main(String[] args) {
        String str = "aaa bbb \"cc dd\" eee fff";
        Matcher matcher = PATTERN.matcher(str);
        List<String> list = new ArrayList<>();
        while (matcher.find()) {
            list.add(matcher.group());
        }
        for (String s : list){
            System.out.println(s);
        }
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM