如何按空格分割字符串但转义引号内的空格(在java中)? [英] how to split string by space but escape spaces inside quotes (in java)?

查看:449
本文介绍了如何按空格分割字符串但转义引号内的空格(在java中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的字符串:

"Video or movie"    "parent"    "Media or entertainment"    "1" "1" "1" "0" "0"

我想用空格拆分它但是引号内的空格应该被忽略。
所以分割的字符串应该是:

I would like to split it by the spaces but the space inside the quote should be ignored. So the splitted strings should be:

"Video or movie"
"parent"
"Media or entertainment"
"1"
...

语言是java。

推荐答案

这应该适合你:

   final String s = "\"Video or movie\"    \"parent\"    \"Media or entertainment\"    \"1\" \"1\" \"1\" \"0\" \"0\"";
        final String[] t = s.split("(?<=\") *(?=\")");
        for (final String x : t) {
            System.out.println(x);
        }

输出:

"Video or movie"
"parent"
"Media or entertainment"
"1"
"1"
"1"
"0"
"0"

这篇关于如何按空格分割字符串但转义引号内的空格(在java中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆